1 package org.enhydra.server; 2 3 import java.io.File ; 4 import java.io.IOException ; 5 import java.lang.reflect.Constructor ; 6 import java.text.DateFormat ; 7 import java.util.Date ; 8 import java.util.HashMap ; 9 import java.util.StringTokenizer ; 10 11 import javax.servlet.Servlet ; 12 13 import org.enhydra.server.util.PathUtil; 14 import org.enhydra.server.util.WebAppXML; 15 import org.enhydra.util.ConfigFileInterface; 16 17 import com.lutris.appserver.server.Application; 18 import com.lutris.appserver.server.httpPresentation.servlet.HttpPresentationServlet; 19 import com.lutris.logging.LogChannel; 20 import com.lutris.logging.Logger; 21 import com.lutris.util.Config; 22 import com.lutris.util.ConfigException; 23 import com.lutris.util.KeywordValueException; 24 31 32 public class AppInfo { 33 34 public static final int ENHYDRA_APP = 0; public static final int WEB_APP = 1; public static final String TYPE_ENHYDRA_APP = "Standard Enhydra Application"; 37 public static final String TYPE_WEB_APP = "Standard Web Application"; 38 private String name; 39 40 private boolean running = false; 41 private int[] connections = null; 42 private HashMap appData = null; 44 private Config config = null; private String description = "N/A"; 47 private String [] additionalClassPaths; 48 private java.util.Date started = null; 49 private String upTime = "N/A"; 50 private String contextPath = "N/A"; 51 private String urlPath = "N/A"; 52 private int appType = WEB_APP; 53 private Application application = null; 55 private String configName = "N/A"; 56 private WebAppXML webAppXML = null; 57 58 64 public AppInfo(String appName,HashMap attributes) 65 throws KeywordValueException,ConfigException,IOException { 66 67 name = appName; 68 appData = attributes; 69 70 urlPath = (String )appData.get("URLPath"); if (urlPath == null) 73 throw new ConfigException("Missing param URLPath in application "+name+", EnhydraServer.xml"); 74 String appsDir = EnhydraServer.getInstance().getAppsDir(); 76 urlPath = PathUtil.makeAbsolutePath(appsDir, urlPath); 77 File webXMLFile = new File (urlPath,"WEB-INF/web.xml"); 78 if(webXMLFile.exists()){ 79 webAppXML = new WebAppXML(webXMLFile.getAbsolutePath()); 80 }else{ 81 webAppXML = null; } 83 if(webAppXML != null){ 85 boolean isEnhydraApp = webAppXML.isEnhydraApplication(); 86 if(isEnhydraApp){ 87 appType = ENHYDRA_APP; 88 }else{ 89 appType = WEB_APP; 90 } 91 } 92 contextPath = (String )appData.get("contextPath"); if (contextPath == null) 95 throw new ConfigException("Missing param contextPath in application "+name+", EnhydraServer.xml"); 96 else if(!contextPath.startsWith("/")){ 97 throw new ConfigException("contextPath must start with '/' in application "+name+", EnhydraServer.xml"); 98 } 99 String appRunning = (String )appData.get("running"); if(appRunning != null && appRunning.equalsIgnoreCase("true")){ 102 running = true; 103 }else{ 104 running = false; 105 } 106 String enabledConns = (String )appData.get("connections"); if(enabledConns != null){ 109 StringTokenizer tokenizer = new StringTokenizer (enabledConns,","); 110 connections = new int[tokenizer.countTokens()]; 111 for(int i=0;i<connections.length;i++){ 112 connections[i] = Integer.parseInt(tokenizer.nextToken().trim()); 113 } 114 } 115 description = (String )appData.get("description"); if(description == null){ 118 description = "N/A"; 119 } 120 if(appType == ENHYDRA_APP){ 122 configName = webAppXML.getConfFilePath(); 124 String webinfDir = PathUtil.makeAbsolutePath(urlPath,"WEB-INF"); 125 File configFile = new File (PathUtil.makeAbsolutePath(webinfDir,configName)); 127 128 ConfigFileInterface configF = createConfigFile(configFile); 132 config = configF.getConfig(); 133 if (config.containsKey("Server.ClassPath")) 135 additionalClassPaths = config.getStrings("Server.ClassPath"); 136 } 137 } 138 145 public AppInfo(Servlet servlet) throws ConfigException, IOException { 146 147 LogChannel logChannel = Logger.getCentralLogger().getChannel("Enhydra"); 148 149 150 urlPath = servlet.getServletConfig().getServletContext().getRealPath(""); 152 155 if (urlPath == null) 156 throw new ConfigException("Automatic started, Can't get URLPath in application "+name); 157 158 File webXMLFile = new File (urlPath,"WEB-INF/web.xml"); 159 if(webXMLFile.exists()){ 160 webAppXML = new WebAppXML(webXMLFile.getAbsolutePath()); 161 }else{ 162 webAppXML = null; } 164 165 String [] pathParts = urlPath.split("[\\\\/]"); 167 contextPath = "/"+pathParts[pathParts.length-1]; 169 configName = webAppXML.getConfFilePath(); 171 String webinfDir = PathUtil.makeAbsolutePath(urlPath,"WEB-INF"); 172 173 File configFile = new File (PathUtil.makeAbsolutePath(webinfDir,configName)); 175 ConfigFileInterface configF = createConfigFile(configFile); 177 180 config = configF.getConfig(); 181 if (config.containsKey("Server.ClassPath")) 183 additionalClassPaths = config.getStrings("Server.ClassPath"); 184 if(servlet instanceof HttpPresentationServlet){ 185 application = ((HttpPresentationServlet) servlet).getApplication(); 186 name = application.getName(); 187 if(name == null){ 188 if(logChannel!=null) 189 logChannel.write(Logger.ERROR," Can't create AppInfo, application name == null !"); 190 throw new ConfigException(" Can't create AppInfo, application name == null !"); 191 } 192 193 setRunning(true); 194 setStarted(new Date ()); 195 setAppType(ENHYDRA_APP); 196 } 197 } 198 public String getName() { 199 return name; 200 } 201 public void setName(String name) { 202 this.name = name; 203 } 204 public void setConfig(com.lutris.util.Config config) { 205 this.config = config; 206 } 207 public com.lutris.util.Config getConfig() { 208 return config; 209 } 210 public void setRunning(boolean running) { 211 this.running = running; 212 } 213 public boolean isRunning() { 214 return running; 215 } 216 public void setConnections(int[] connections) { 217 this.connections = connections; 218 } 219 public int[] getConnections() { 220 return connections; 221 } 222 private void log(String message){ 224 DateFormat d = DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT); 225 System.err.println(d.format(new Date ())+" EnhydraServer, AppInfo"+message); 226 } 227 228 public void setDescription(String description) { 229 this.description = description; 230 } 231 public String getDescription() { 232 return description; 233 } 234 public void setAdditionalClassPaths(String [] additionalClassPaths) { 235 this.additionalClassPaths = additionalClassPaths; 236 } 237 public String [] getAdditionalClassPaths() { 238 return additionalClassPaths; 239 } 240 public void setStarted(java.util.Date started) { 241 this.started = started; 242 } 243 public java.util.Date getStarted() { 244 return started; 245 } 246 250 public long getUpTime() { 251 if(started != null) { 252 long upTimeSec = (System.currentTimeMillis()-started.getTime())/1000; 253 return upTimeSec; 254 } 255 else 256 return 0; 257 } 258 public void setContextPath(String contextPath) { 259 this.contextPath = contextPath; 260 } 261 public String getContextPath() { 262 return contextPath; 263 } 264 public void setUrlPath(String urlPath) { 265 this.urlPath = urlPath; 266 } 267 public String getUrlPath() { 268 return urlPath; 269 } 270 public String getType() { 271 String type = "N/A"; 272 switch(getAppType()){ 273 case ENHYDRA_APP: 274 type = TYPE_ENHYDRA_APP; 275 break; 276 case WEB_APP: 277 type = TYPE_WEB_APP; 278 break; 279 } 280 return type; 281 } 282 public int getAppType(){ 283 return appType; 284 } 285 public void setAppType(int t){ 286 appType = t; 287 } 288 public Application getApplication() { 289 return application; 290 } 291 public void setApplication(Application application) { 292 this.application = application; 293 } 294 public void setConfigName(String configName) { 295 this.configName = configName; 296 } 297 public String getConfigName() { 298 return configName; 299 } 300 public WebAppXML getWebAppXML(){ 301 return webAppXML; 302 } 303 public void setWebAppXML(WebAppXML w){ 304 webAppXML = w; 305 } 306 307 private ConfigFileInterface createConfigFile (File configFile) 308 throws ConfigException { String configFileClass = null; 310 if (webAppXML != null) 311 configFileClass = webAppXML.getConfFileClass(); 312 if (configFileClass == null) 313 configFileClass = EnhydraServer.DEFAULT_CONF_FILE_CLASS; 314 315 Class classObj = null; 316 Class [] classParam = null; 317 try { 318 classObj = Class.forName(configFileClass, true, this.getClass().getClassLoader()); 319 classParam = new Class [1]; 320 classParam[0] = Class.forName("java.io.File"); 321 } 322 catch (ClassNotFoundException ex){ 323 throw new ConfigException("Can't create application configuration file: ClassNotFound error"); 324 } 325 326 Constructor constr; 327 try { 328 constr = classObj.getConstructor(classParam); 329 } 330 catch (NoSuchMethodException ex){ 331 throw new ConfigException("Error in constructor: can't create application configuration file."); 332 } 333 Object [] arguments = new Object [1]; 334 335 arguments[0] = (Object )(configFile); 336 337 ConfigFileInterface configF = null; 338 try { 339 configF = (ConfigFileInterface)constr.newInstance(arguments); 340 } 341 catch (Exception ex){ 342 throw new ConfigException("Can't create application configuration file."); 343 } 344 return configF; 345 346 } 347 } | Popular Tags |