1 18 19 package de.gulden.framework.amoda.generic.core; 20 21 import de.gulden.framework.amoda.generic.metadata.*; 22 import de.gulden.framework.amoda.generic.option.*; 23 import de.gulden.framework.amoda.model.core.*; 24 import de.gulden.framework.amoda.model.core.ApplicationEnvironment; 25 import de.gulden.framework.amoda.model.core.Feature; 26 import de.gulden.framework.amoda.model.data.*; 27 import de.gulden.framework.amoda.model.data.Value; 28 import de.gulden.framework.amoda.model.interaction.*; 29 import de.gulden.framework.amoda.model.metadata.*; 30 import de.gulden.framework.amoda.model.option.*; 31 import de.gulden.util.Toolbox; 32 import de.gulden.util.xml.*; 33 import de.gulden.util.xml.serializer.*; 34 import de.gulden.util.xml.serializer.XMLSerializable; 35 import java.lang.*; 36 import java.net.*; 37 import java.util.*; 38 import javax.xml.parsers.*; 39 import org.w3c.dom.*; 40 41 47 public abstract class GenericApplicationEnvironment implements ApplicationEnvironment, XMLSerializable { 48 49 53 public static final Object [][] XML_COLLECTION_ELEMENTS = {{"features",GenericFeature.FEATURE_MEMBERS}}; 54 55 public static final String VERSION = "0.2"; 56 57 public static String DEFAULT_LOGFILE = "./application.log"; 58 59 public static String DEFAULT_PROPERTIES = "./application.properties"; 60 61 public static String NL = System.getProperty("line.separator"); 62 63 public static String CONFIGURATION_PERSISTENT_OPTIONS = "persistent-options"; 64 65 public static String CONFIGURATION_BATCH_MODE = "batch-mode"; 66 67 public static String CONFIGURATION_INTERACTION_MODE = "interaction-mode"; 68 69 public Metadata metadata; 70 71 public Map features; 72 73 public Options options; 74 75 protected ArgsParser argsParser; 76 77 protected Collection batchCommands; 78 79 protected Value[] inputValues; 80 81 protected Workspace workspace; 82 83 protected GenericApplicationEnvironmentFactory factory; 84 85 protected GenericApplication genericApplication; 86 87 protected LogFile logFile; 88 89 90 94 public GenericApplicationEnvironment() { 95 options=new GenericOptions(); 96 } 97 98 99 103 public GenericApplicationEnvironmentFactory getFactory() { 104 return factory; 105 } 106 107 public void setFactory(GenericApplicationEnvironmentFactory genericApplicationEnvironmentFactory) { 108 this.factory = genericApplicationEnvironmentFactory; 109 } 110 111 public GenericApplication getGenericApplication() { 112 return genericApplication; 113 } 114 115 public void setGenericApplication(GenericApplication genericApplication) { 116 if (this.genericApplication != genericApplication) { 117 this.genericApplication = genericApplication; 118 if (genericApplication != null) genericApplication.setGenericApplicationEnvironment(this); 119 } 120 } 121 122 public LogFile getLogFile() { 123 return logFile; 124 } 125 126 public void setLogFile(LogFile logFile) { 127 this.logFile = logFile; 128 } 129 130 public void launch(Application application) { 131 GenericApplication genericApplication=(GenericApplication)application; setGenericApplication(genericApplication); 133 try { 134 setArgsParser(genericApplication.createArgsParser()); setWorkspace(genericApplication.createWorkspace()); 136 137 genericApplication.init(this); launchAfterInit(genericApplication); 139 } catch (Throwable t) { 140 getGenericApplication().fatalError(t); 141 } 142 } 143 144 public Message createDefaultMessage(String title, String text, int type, Metadata metadata) { 145 de.gulden.framework.amoda.generic.interaction.GenericMessage m=new de.gulden.framework.amoda.generic.interaction.GenericMessage(); 146 m.setParent(getGenericApplication()); 147 m.setTitle(title); 148 m.setText(text); 149 m.setType(type); 150 return m; 151 } 152 153 public ErrorMessage createDefaultErrorMessage(String text, Throwable throwable, boolean exitApplication, Metadata metadata) { 154 de.gulden.framework.amoda.generic.interaction.GenericErrorMessage m=new de.gulden.framework.amoda.generic.interaction.GenericErrorMessage(); 155 m.setParent(getGenericApplication()); 156 m.setTitle("Error"); 157 m.setText(text); 158 m.setCause(throwable); 159 m.setExitApplication(exitApplication); 160 return m; 161 } 162 163 public Question createDefaultQuestion(String title, String text, String answerOptionsIds, Metadata metadata) { 164 de.gulden.framework.amoda.generic.interaction.GenericQuestion m=new de.gulden.framework.amoda.generic.interaction.GenericQuestion(); 165 m.setParent(getGenericApplication()); 166 m.setTitle(title); 167 m.setText(text); 168 m.setAnswers(answerOptionsIds); 169 return m; 170 } 171 172 public Dialog createDefaultDialog(String title, String text, Options options, Metadata metadata) { 173 de.gulden.framework.amoda.generic.interaction.GenericDialog m=new de.gulden.framework.amoda.generic.interaction.GenericDialog(); 174 m.setParent(getGenericApplication()); 175 m.setTitle(title); 176 m.setText(text); 177 m.setOptions(options); 178 return m; 179 } 180 181 public Wizard createDefaultWizard(String title, Collection dialogs, Metadata metadata) { 182 de.gulden.framework.amoda.generic.interaction.GenericWizard m=new de.gulden.framework.amoda.generic.interaction.GenericWizard(); 183 m.setParent(getGenericApplication()); 184 m.setTitle(title); 185 m.getDialogs().addAll(dialogs); return m; 187 } 188 189 public LogMessage createDefaultLogMessage(String text, Object source, int type, Metadata metadata) { 190 de.gulden.framework.amoda.generic.interaction.GenericLogMessage m=new de.gulden.framework.amoda.generic.interaction.GenericLogMessage(); 191 m.setParent(getGenericApplication()); 192 m.setText(text); 193 m.setSource(source); 194 m.setType(type); 195 return m; 196 } 197 198 public abstract void doMessage(Message message); 199 200 public void doLogMessage(LogMessage logMessage) { 201 LogMessagePerformer log=getLogMessagePerformer(); 202 if (log!=null) { 203 log.doLogMessage(logMessage); 204 } 205 try { 206 if (logMessage.getApplication().getOptions().getBoolean("show-log-messages")) { 207 doMessage(logMessage); 208 return; 209 } 210 } catch (IllegalOptionError ioe) { 211 } 213 if (logMessage.getApplication().isVerbose()) { 214 System.out.println("[log] "+logMessage.getText()); 215 } 216 } 217 218 public abstract void doErrorMessage(ErrorMessage errorMessage); 219 220 public abstract void doQuestion(Question question); 221 222 public abstract void doDialog(Dialog dialog); 223 224 public abstract void doWizard(Wizard wizard); 225 226 public OptionEntry createDefaultOption(String id, Object initialValue, Metadata metadata) { 227 de.gulden.framework.amoda.generic.option.GenericOptionEntry o=new de.gulden.framework.amoda.generic.option.GenericOptionEntry(); 228 o.setParent(getGenericApplication()); 229 o.setId(id); 230 Class initialType; 231 if (initialValue!=null) { 232 initialType=initialValue.getClass(); 233 } else { 234 initialType=String .class; 235 } 236 o.setType(initialType); 237 ((de.gulden.framework.amoda.generic.data.GenericValue)o.getValue(OptionEntry.STATE_DEFAULT)).set(initialValue); 238 o.setMetadata(metadata); 239 return o; 240 } 241 242 public OptionChoice createDefaultOptionsChoice(String id, Option[] options, Metadata metadata) { 243 de.gulden.framework.amoda.generic.option.GenericOptionChoice o=new de.gulden.framework.amoda.generic.option.GenericOptionChoice(); 244 o.setParent(getGenericApplication()); 245 o.setId(id); 246 for (int i=0;i<options.length;i++) { 247 o.getEntries().put(options[i].getId(),options[i]); 248 } 249 o.setMetadata(metadata); 250 return o; 251 } 252 253 public Options createDefaultOptions() { 254 de.gulden.framework.amoda.generic.option.GenericOptions o=new de.gulden.framework.amoda.generic.option.GenericOptions(); 255 o.setParent(getGenericApplication()); 256 return o; 257 } 258 259 public Metadata createDefaultMetadata(String name, String title, String description, String author, String copyright, String email, String web) { 260 de.gulden.framework.amoda.generic.metadata.GenericMetadata m=new de.gulden.framework.amoda.generic.metadata.GenericMetadata(); 261 if (name!=null) m.set("name",name); 262 if (name!=null)m.set("longname",name); 263 if (title!=null)m.set("title",title); 264 if (description!=null)m.set("description",description); 265 if (author!=null)m.set("author",author); 266 if (copyright!=null)m.set("copyright",copyright); 267 if (email!=null)m.set("email",email); 268 if (web!=null)m.set("web",web); 269 return m; 270 } 271 272 public Metadata createDefaultMetadata(String name, String title, String description) { 273 return createDefaultMetadata(name,title,description,null,null,null,null); 274 } 275 276 public Metadata createDefaultMetadata(String title, String description) { 277 return createDefaultMetadata(null,title,description); 278 } 279 280 public Metadata createDefaultMetadata(String description) { 281 return createDefaultMetadata(null,description); 282 } 283 284 public DocumentBuilder getDefaultDocumentBuilder() { 285 return ((GenericApplicationEnvironmentFactory)getFactory()).getDocumentBuilder(); 286 } 287 288 public Metadata parseXMLMetadata(Element element, XMLSerializer serializer) throws XMLException { 289 Element metadataTag=de.gulden.util.xml.XMLToolbox.getChild(element,"metadata"); 290 if (metadataTag!=null) { 291 de.gulden.framework.amoda.generic.metadata.GenericMetadata metadata=new de.gulden.framework.amoda.generic.metadata.GenericMetadata(); 292 serializer.xmlDeserialize(metadata,metadataTag); 293 return metadata; 294 } else { 295 return new de.gulden.framework.amoda.generic.metadata.GenericMetadata(); } 297 } 298 299 public Options parseXMLOptions(Element element, XMLSerializer serializer) throws XMLException { 300 Element optionsTag=de.gulden.util.xml.XMLToolbox.getChild(element,"options"); 301 if (optionsTag!=null) { 302 de.gulden.framework.amoda.generic.option.GenericOptions options=new de.gulden.framework.amoda.generic.option.GenericOptions(); 303 options.setParent(getGenericApplication()); 304 serializer.xmlDeserialize(options,optionsTag); 305 return options; 306 } else { 307 return null; 308 } 309 } 310 311 public LogMessagePerformer getLogMessagePerformer() { 312 return getLogFile(); 313 } 314 315 public Metadata getMetadata() { 316 return metadata; 317 } 318 319 public void setMetadata(Metadata _metadata) { 320 metadata = _metadata; 321 } 322 323 public ArgsParser getArgsParser() { 324 return argsParser; 325 } 326 327 public void setArgsParser(ArgsParser _argsParser) { 328 argsParser = _argsParser; 329 } 330 331 public Collection getBatchCommands() { 332 return batchCommands; 333 } 334 335 public void setBatchCommands(Collection _batchCommands) { 336 batchCommands = _batchCommands; 337 } 338 339 public Workspace getWorkspace() { 340 return workspace; 341 } 342 343 public void setWorkspace(Workspace _workspace) { 344 workspace = _workspace; 345 } 346 347 public Map getFeatures() { 348 return features; 349 } 350 351 public void setFeatures(Map _features) { 352 features = _features; 353 } 354 355 public abstract void doExit(GenericApplication application, int code); 356 357 public Workspace createDefaultWorkspace() { 358 return null; 360 } 361 362 public Value[] getInputValues() { 363 return inputValues; 364 } 365 366 public void setInputValues(Value[] _inputValues) { 367 inputValues = _inputValues; 368 } 369 370 public void exit(int exitCode) { 371 System.exit(exitCode); 372 } 373 374 public void exit() { 375 exit(0); 376 } 377 378 public void doAbout() { 379 getGenericApplication().createAboutMessage().perform(); 380 } 381 382 public void doUsage(boolean exitApplication) { 383 de.gulden.framework.amoda.model.interaction.Message m=getGenericApplication().createUsageMessage(); 384 if (m!=null) { 385 String t = m.getText(); 386 t += "Use option '-help' to see the list of options."+NL; 387 ((de.gulden.framework.amoda.generic.interaction.GenericMessage)m).setText(t); 388 m.perform(); 389 } 390 if (exitApplication) { 391 getGenericApplication().exit(); 392 } 393 } 394 395 public void doHelp() { 396 de.gulden.framework.amoda.model.interaction.Message m=getGenericApplication().createHelpMessage(); 397 if (m!=null) { 398 m.perform(); 399 } 400 } 401 402 protected void launchAfterInit(GenericApplication genericApplication) { 403 prepareApplicationWelcome(); 404 if (!genericApplication.isQuiet()) { 405 genericApplication.welcome(); 406 } 407 if (genericApplication.getOptions().getBoolean("help")) { 408 genericApplication.help(); 409 } else { 410 if (genericApplication.getOptions().getBoolean("use-logging")) { 411 setLogFile(createLogFile()); 412 } 413 prepareApplicationStart(); 414 launchStart(genericApplication); 416 } 418 } 419 420 protected LogFile createLogFile() { 421 java.io.File f=new java.io.File (getLogFilename()); 422 LogFile logFile=new LogFile(); 423 logFile.setFile(f); 424 logFile.open(); 425 return logFile; 426 } 427 428 protected String getLogFilename() { 429 String n=null; 430 n=getGenericApplication().getOptions().getString("logfile"); 431 if (n==null) { 432 n=getDefaultLogFilename(); 433 } 434 return n; 435 } 436 437 protected String getDefaultLogFilename() { 438 String n=System.getProperties().getProperty("amoda.default.logfile",DEFAULT_LOGFILE); 439 return n; 440 } 441 442 protected void initApplicationOptions(GenericApplication application) { 443 for (Iterator it=this.features.values().iterator();it.hasNext();) { 445 Feature f=(Feature)it.next(); 446 Object alreadyExisting=application.get(f.getId(),false); if (alreadyExisting==null) { 448 application.add(f); } 450 } 451 for (Iterator it=this.options.getAll().values().iterator();it.hasNext();) { 452 Option o=(Option)it.next(); 453 Object alreadyExisting=application.getOptions().get(o.getId(),false); 454 if (alreadyExisting==null) { 455 ((GenericOptions)application.getOptions()).add(o); 456 } 457 } 458 setBatchCommands(Collections.singletonList(application.getFeature("default"))); ArgsParser argsParser=getArgsParser(); 460 if (argsParser!=null) { 461 if (options.getBoolean("batch-mode")) { Collection features = application.getAvailableFeatures(); 469 Collection c = argsParser.parseBatchCommands(features); if (!c.isEmpty()) { 471 setBatchCommands(c); 472 } 473 } 474 GenericOptions options = (GenericOptions)application.getOptions(); 475 argsParser.parseOptions(options); 476 setInputValues(argsParser.parseInputValues()); 477 } 478 java.io.File file=application.getOptions().getFile("properties"); 480 if ((file==null) && (application.getOptions().getBoolean(CONFIGURATION_PERSISTENT_OPTIONS))) { String filename="./"+application.getId()+".properties"; 482 file=new java.io.File (filename); 483 application.getOptions().getOptionEntry("properties").getValue(de.gulden.framework.amoda.model.option.Option.STATE_CURRENT).set(file); 484 } 485 if (file != null) { 486 ((GenericOptions)application.getOptions()).loadFromProperties(file); 487 } 488 if (argsParser != null) { 490 argsParser.parseOptions((GenericOptions)application.getOptions()); 491 } 492 } 493 494 protected String getPropertiesFilename() { 495 String n=null; 496 n=getGenericApplication().getOptions().getString("properties"); if (n==null) { 498 n=getDefaultPropertiesFilename(); 499 } 500 return n; 501 } 502 503 protected String getDefaultPropertiesFilename() { 504 String n=System.getProperties().getProperty("amoda.properties",DEFAULT_PROPERTIES); 505 return n; 506 } 507 508 protected void prepareApplicationWelcome() { 509 } 511 512 protected void prepareApplicationStart() { 513 } 515 516 protected void launchStart(GenericApplication application) { 517 application.start(); 518 } 519 520 521 525 public static void main(String [] args) throws Exception { 526 String applicationClassname = System.getProperty("application", "de.gulden.framework.amoda.generic.GenericApplicationDummy"); 527 String environmentFactoryClassname = System.getProperty("application.environment.factory", "de.gulden.framework.amoda.environment.commandline.CommandLineApplicationEnvironmentFactory"); 528 invoke(applicationClassname, args, environmentFactoryClassname); 529 } 530 531 public static void invoke(String applicationClassname, String [] args, String environmentFactoryClassname) throws Exception { 532 Application application = (Application)Toolbox.instantiateClass(applicationClassname, Application.class); 533 ApplicationEnvironmentFactory factory = (ApplicationEnvironmentFactory)Toolbox.instantiateClass(environmentFactoryClassname, ApplicationEnvironmentFactory.class); 534 invoke(application, args, factory); 535 } 536 537 public static void invoke(Class applicationClass, String [] args, Class environmentFactoryClass) throws Exception { 538 invoke((Application)applicationClass.newInstance(), args, (ApplicationEnvironmentFactory)environmentFactoryClass.newInstance()); 539 } 540 541 public static void invoke(Application application, String [] args, ApplicationEnvironmentFactory environmentFactory) { 542 environmentFactory.setArgs(args); 543 environmentFactory.createApplicationEnvironment().launch(application); 544 } 545 546 public static URL getXMLResourceForClass(Class cls) { 547 String resName = cls.getName(); 548 resName = resName.replace('.', '/'); 549 resName += ".xml"; 550 URL res = Toolbox.findResource(resName, cls.getClassLoader()); 551 return res; 552 } 553 554 } | Popular Tags |