00001 package org.openmobileis.oscar;
00002
00003 import java.io.BufferedReader;
00004 import java.io.File;
00005 import java.io.FileNotFoundException;
00006 import java.io.IOException;
00007 import java.io.InputStream;
00008 import java.io.InputStreamReader;
00009 import java.net.MalformedURLException;
00010 import java.net.URL;
00011 import java.util.Enumeration;
00012 import java.util.Properties;
00013
00014 import org.openmobileis.common.util.PropertiesManager;
00015 import org.ungoverned.oscar.Noscar;
00016 import org.ungoverned.oscar.cache.DefaultBundleCache;
00017 import org.ungoverned.oscar.util.CaseInsensitiveMap;
00018 import org.ungoverned.oscar.util.MutablePropertyResolverImpl;
00019
00020 public class StartOscar {
00021
00022 public static final String CONFIG_PROPERTIES_FILE_VALUE = "oscar.properties";
00023
00024
00025 public static void main(String[] args) {
00026
00027 System.out.println("user.dir : "+System.getProperty("user.dir"));
00028 Properties configProps = null;
00029
00030
00031 if (args != null && args.length > 0) {
00032 configProps = StartOscar.readConfigProperties(args[0]);
00033 } else {
00034 configProps = StartOscar.readConfigProperties();
00035 }
00036
00037
00038 String profileName = configProps.getProperty(DefaultBundleCache.CACHE_PROFILE_PROP);
00039
00040
00041 String profileDirName = configProps.getProperty(DefaultBundleCache.CACHE_PROFILE_DIR_PROP);
00042
00043
00044 System.out.println("\nWelcome to ObjectWeb Open Mobile IS Oscar.");
00045 System.out.println("==========================================\n");
00046
00047
00048
00049 if ((profileName == null) && (profileDirName == null))
00050 {
00051 System.out.print("Enter profile name: ");
00052 BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
00053 try
00054 {
00055 profileName = in.readLine();
00056 }
00057 catch (IOException ex)
00058 {
00059 System.err.println("Could not read input.");
00060 System.exit(-1);
00061 }
00062 System.out.println("");
00063 if (profileName.length() != 0)
00064 {
00065 configProps.setProperty(DefaultBundleCache.CACHE_PROFILE_PROP, profileName);
00066 }
00067 }
00068
00069
00070 if ((profileDirName == null) && (profileName.length() == 0))
00071 {
00072 System.err.println("You must specify a profile name or directory.");
00073 System.exit(-1);
00074 }
00075
00076 try
00077 {
00078
00079 Noscar oscar = new Noscar();
00080 oscar.start(
00081 new MutablePropertyResolverImpl(new CaseInsensitiveMap(configProps)),
00082 null);
00083 }
00084 catch (Exception ex)
00085 {
00086 System.err.println("Could not create Oscar: " + ex);
00087 ex.printStackTrace();
00088 System.exit(-1);
00089 }
00090 }
00091
00092 public static Properties readConfigProperties() {
00093 return readConfigProperties(null);
00094 }
00095
00096 public static Properties readConfigProperties(String userdir)
00097 {
00098
00099 URL propURL = null;
00100 String propFileName = null;
00101 try
00102 {
00103 if (userdir != null && userdir.trim().length() > 0) {
00104 System.setProperty("user.dir", userdir.trim());
00105 }
00106 propURL = new File(System.getProperty("user.dir") + File.separator + "WEB-INF" + File.separator + "conf" + File.separator + "properties" + File.separator + CONFIG_PROPERTIES_FILE_VALUE).toURL();
00107 propFileName = System.getProperty("user.dir") + File.separator + "WEB-INF" + File.separator + "conf" + File.separator + "properties" + File.separator + CONFIG_PROPERTIES_FILE_VALUE;
00108 System.out.println("propFileName : "+propFileName);
00109 }
00110 catch (MalformedURLException ex)
00111 {
00112 System.err.print("StartOscar: " + ex);
00113 return null;
00114 }
00115
00116
00117 Properties props = new Properties();
00118 InputStream is = null;
00119 try
00120 {
00121
00122
00123
00124 props = PropertiesManager.getManager().getPropertiesFromPath(propFileName);
00125
00126
00127 }
00128 catch (FileNotFoundException ex)
00129 {
00130
00131 }
00132 catch (Exception ex)
00133 {
00134 System.err.println(
00135 "StartOscar: Error loading system properties from " + propURL);
00136 System.err.println("StartOscar: " + ex);
00137 try
00138 {
00139 if (is != null) is.close();
00140 }
00141 catch (IOException ex2)
00142 {
00143
00144 }
00145 return null;
00146 }
00147
00148
00149 for (Enumeration e = props.propertyNames(); e.hasMoreElements(); )
00150 {
00151 String name = (String) e.nextElement();
00152 props.setProperty(name, substVars((String) props.getProperty(name)));
00153 }
00154
00155 return props;
00156 }
00157
00158 private static final String DELIM_START = "${";
00159 private static final char DELIM_STOP = '}';
00160 private static final int DELIM_START_LEN = 2;
00161 private static final int DELIM_STOP_LEN = 1;
00162
00163 public static String substVars(String val)
00164 throws IllegalArgumentException
00165 {
00166 StringBuffer sbuf = new StringBuffer();
00167
00168 if (val == null)
00169 {
00170 return val;
00171 }
00172
00173 int i = 0;
00174 int j, k;
00175
00176 while (true)
00177 {
00178 j = val.indexOf(DELIM_START, i);
00179 if (j == -1)
00180 {
00181 if (i == 0)
00182 {
00183 return val;
00184 }
00185 else
00186 {
00187 sbuf.append(val.substring(i, val.length()));
00188 return sbuf.toString();
00189 }
00190 }
00191 else
00192 {
00193 sbuf.append(val.substring(i, j));
00194 k = val.indexOf(DELIM_STOP, j);
00195 if (k == -1)
00196 {
00197 throw new IllegalArgumentException(
00198 '"' + val +
00199 "\" has no closing brace. Opening brace at position "
00200 + j + '.');
00201 }
00202 else
00203 {
00204 j += DELIM_START_LEN;
00205 String key = val.substring(j, k);
00206
00207 String replacement = System.getProperty(key, null);
00208 if (replacement != null)
00209 {
00210 sbuf.append(replacement);
00211 }
00212 i = k + DELIM_STOP_LEN;
00213 }
00214 }
00215 }
00216 }
00217
00218 }