001: package org.gridsphere.services.core.customization.impl;
002:
003: import org.apache.commons.logging.Log;
004: import org.apache.commons.logging.LogFactory;
005: import org.apache.oro.text.perl.Perl5Util;
006: import org.apache.oro.text.regex.MatchResult;
007: import org.gridsphere.portlet.service.PortletServiceUnavailableException;
008: import org.gridsphere.portlet.service.spi.PortletServiceConfig;
009: import org.gridsphere.portlet.service.spi.PortletServiceProvider;
010: import org.gridsphere.services.core.customization.SettingsService;
011: import org.gridsphere.servlets.GridSphereServlet;
012:
013: import javax.naming.Context;
014: import javax.naming.InitialContext;
015: import javax.naming.NamingException;
016: import java.io.File;
017: import java.io.FileInputStream;
018: import java.io.FileOutputStream;
019: import java.io.IOException;
020:
021: public class SettingsServiceImpl implements PortletServiceProvider,
022: SettingsService {
023:
024: private Log log = LogFactory.getLog(GridSphereServlet.class);
025: private String settingsPath = System.getProperty("user.home")
026: + File.separator + ".gridsphere";
027: private Perl5Util util = new Perl5Util();
028:
029: /**
030: * Checks if the JNDI variable for storing gs settings data is set, if not the default is assumed
031: *
032: * @param config
033: * @throws PortletServiceUnavailableException
034: *
035: */
036: public void init(PortletServiceConfig config)
037: throws PortletServiceUnavailableException {
038:
039: log.debug("In Settings");
040:
041: try {
042: Context initCtx = new InitialContext();
043: javax.naming.Context env = (Context) initCtx
044: .lookup("java:comp/env");
045: settingsPath = (String) env.lookup("gridspheresettingsdir");
046:
047: //GPF-480 feature: change ${ENV.SYS_VAR_NAME} to value of system environment variable with named "SYS_VAR_NAME"
048: while (util
049: .match("m/\\$\\{ENV\\.([^}]+)\\}/", settingsPath)) {
050: MatchResult matchResult = util.getMatch();
051: String systemEnvironmentVariableName = matchResult
052: .group(1);
053: //can cause NPE if system environment variable was not set
054: settingsPath = settingsPath.replaceAll("\\$\\{ENV\\."
055: + systemEnvironmentVariableName + "\\}", System
056: .getenv(systemEnvironmentVariableName).replace(
057: '\\', '/'));
058: }
059: log.info("Got config settings from JNDI");
060: } catch (NamingException e) {
061: // it does not exist, which is ok as well, we just use the default
062: settingsPath = System.getProperty("user.home")
063: + File.separator + ".gridsphere";
064: } catch (NullPointerException e) {
065: // it exists, but some of system environment variable used in it's value was not set
066: settingsPath = System.getProperty("user.home")
067: + File.separator + ".gridsphere";
068: }
069: // check if the path exist, if not create it and copy the template files (from WEB-INF/CustomPortal) to it
070: File path = new File(settingsPath);
071: log.info("GridSphere Settings will be saved in: "
072: + settingsPath);
073: if (!path.exists()) {
074: path.mkdirs();
075: log.info("GridSphere ConfigDir did not exist, created.");
076: try {
077: copyFiles(config.getServletContext().getRealPath(
078: "/WEB-INF/CustomPortal"), settingsPath);
079: log.info("Copy files to directory");
080: } catch (IOException e) {
081: log
082: .error("Could not copy files to defined destination: "
083: + config
084: .getServletContext()
085: .getRealPath(
086: "/WEB-INF/CustomPortal")
087: + " to " + settingsPath);
088: }
089:
090: }
091: }
092:
093: public void destroy() {
094: //To change body of implemented methods use File | Settings | File Templates.
095: }
096:
097: public String getSettingsPath() {
098: return settingsPath;
099: }
100:
101: public String getRealSettingsPath(String path) {
102: String result = settingsPath + File.separator + path;
103: //result = result.replaceAll("/", File.separator);
104: File f = new File(result);
105: if (f.isDirectory() && !result.endsWith(File.separator))
106: result = result + File.separator;
107: return result;
108: }
109:
110: private void copyFiles(String strPath, String dstPath)
111: throws IOException {
112: File src = new File(strPath);
113: File dest = new File(dstPath);
114:
115: if (src.isDirectory()) {
116: //if(dest.exists()!=true)
117: dest.mkdirs();
118: String list[] = src.list();
119:
120: for (int i = 0; i < list.length; i++) {
121: String dest1 = dest.getAbsolutePath() + File.separator
122: + list[i];
123: String src1 = src.getAbsolutePath() + File.separator
124: + list[i];
125: copyFiles(src1, dest1);
126: }
127: } else {
128: FileInputStream fin = new FileInputStream(src);
129: FileOutputStream fout = new FileOutputStream(dest);
130: int c;
131: while ((c = fin.read()) >= 0)
132: fout.write(c);
133: fin.close();
134: fout.close();
135: }
136: }
137: }
|