1 17 18 package org.pentaho.core.system; 19 20 import java.io.File ; 21 import java.io.FileNotFoundException ; 22 import java.util.ArrayList ; 23 import java.util.Collections ; 24 import java.util.HashMap ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.Locale ; 28 import java.util.Map ; 29 30 import org.dom4j.Document; 31 import org.dom4j.DocumentHelper; 32 import org.dom4j.Element; 33 import org.dom4j.Node; 34 import org.pentaho.core.publisher.IPentahoPublisher; 35 import org.pentaho.core.repository.IContentRepository; 36 import org.pentaho.core.repository.IRuntimeRepository; 37 import org.pentaho.core.repository.ISolutionRepository; 38 import org.pentaho.core.runtime.IActionParameter; 39 import org.pentaho.core.runtime.IBackgroundExecution; 40 import org.pentaho.core.runtime.IRuntimeContext; 41 import org.pentaho.core.session.IPentahoSession; 42 import org.pentaho.core.session.StandaloneSession; 43 import org.pentaho.core.solution.HttpSessionParameterProvider; 44 import org.pentaho.core.solution.IParameterProvider; 45 import org.pentaho.core.solution.ISolutionEngine; 46 import org.pentaho.core.solution.SimpleOutputHandler; 47 import org.pentaho.core.solution.SimpleParameterProvider; 48 import org.pentaho.core.ui.IPentahoUrlFactory; 49 import org.pentaho.core.ui.IXMLComponent; 50 import org.pentaho.core.ui.SimpleUrlFactory; 51 import org.pentaho.core.util.IUITemplater; 52 import org.pentaho.messages.Messages; 53 import org.pentaho.messages.util.LocaleHelper; 54 import org.pentaho.util.VersionHelper; 55 import org.pentaho.util.logging.ILogger; 56 import org.pentaho.util.logging.Logger; 57 58 public class PentahoSystem { 59 60 public static final boolean debug = true; 61 62 public static final boolean ignored = false; 64 67 public static int loggingLevel = ILogger.ERROR; 68 69 private static IApplicationContext applicationContext; 70 71 protected static final String CONTENT_REPOSITORY = "IContentRepository"; 73 protected static final String RUNTIME_REPOSITORY = "IRuntimeRepository"; 75 private static final String SOLUTION_REPOSITORY = "ISolutionRepository"; 77 protected static final String SOLUTION_ENGINE = "ISolutionEngine"; 79 public static final String BACKGROUND_EXECUTION = "IBackgroundExecution"; 81 private static final String SCOPE_GLOBAL = "global"; 83 protected static final String SCOPE_SESSION = "session"; 85 protected static final String SCOPE_REQUEST = "request"; 87 private static Map globalObjectsMap = Collections.synchronizedMap(new HashMap ()); 88 89 private static HashMap globalAttributes; 90 91 private static SimpleParameterProvider globalParameters; 92 93 private static ISystemSettings systemSettings; 94 95 private static List publishers; 96 97 private static List listeners; 98 99 private static Map scopeMap; 100 101 private static Map classNameMap; 102 103 private static Map initializationFailureDetailsMap = new HashMap (); 104 105 private static HashMap sessionCreateMap; 106 107 private static IRuntimeRepository runtimeRepository; 108 private static IContentRepository contentRepository; 109 110 111 public static final int SYSTEM_NOT_INITIALIZED = -1; 112 public static final int SYSTEM_INITIALIZED_OK = 0; 113 public static final int SYSTEM_LISTENERS_FAILED = (int) Math.pow(2, 0); public static final int SYSTEM_OBJECTS_FAILED = (int) Math.pow(2, 1); public static final int SYSTEM_PUBLISHERS_FAILED = (int) Math.pow(2, 2); public static final int SYSTEM_AUDIT_FAILED = (int) Math.pow(2, 3); public static final int SYSTEM_PENTAHOXML_FAILED = (int) Math.pow(2, 4); public static final int SYSTEM_SETTINGS_FAILED = (int) Math.pow(2, 5); 120 private static int initializedStatus = SYSTEM_NOT_INITIALIZED; 121 122 124 127 public static synchronized boolean retrySystemInit() { 128 IApplicationContext appContext = applicationContext; 129 publishers.clear(); 130 listeners.clear(); 131 scopeMap.clear(); 132 sessionCreateMap.clear(); 133 globalAttributes.clear(); 134 globalParameters = null; 135 publishers = null; 136 scopeMap = null; 137 sessionCreateMap = null; 138 globalAttributes = null; 139 return init( appContext ); 140 } 141 142 public static boolean init(IApplicationContext pApplicationContext) { 143 return init( pApplicationContext, null ); 144 } 145 146 public static boolean init(IApplicationContext pApplicationContext, Map listenerMap) { 147 initializedStatus = SYSTEM_INITIALIZED_OK; 148 VersionHelper.setVersion(Messages.getString("PentahoSystem.SYSTEM_PLATFORM_VERSION")); VersionHelper.setBuild(Messages.getString("PentahoSystem.SYSTEM_PLATFORM_BUILD_NUMBER")); 151 publishers = new ArrayList (); 152 listeners = new ArrayList (); 153 scopeMap = new HashMap (); 154 classNameMap = new HashMap (); 155 sessionCreateMap = new HashMap (); 156 globalAttributes = new HashMap (); 157 globalParameters = new SimpleParameterProvider(globalAttributes); 158 159 applicationContext = pApplicationContext; 160 161 String propertyPath = applicationContext.getSolutionPath( "" ); propertyPath = propertyPath.replaceAll( "\\\\", "/" ); System.setProperty( "pentaho.solutionpath", propertyPath ); 165 if (LocaleHelper.getLocale() == null) { 166 LocaleHelper.setLocale(Locale.getDefault()); 167 } 168 169 String systemSettingsPath = applicationContext.getSolutionPath("system"); File systemSettingsDir = new File (systemSettingsPath); 171 if (!systemSettingsDir.exists() || !systemSettingsDir.isDirectory()) { 172 Logger.error(PentahoSystem.class.getName(), Messages.getString("PentahoSystem.ERROR_0001_SYSTEM_DIRECTORY_INVALID"), new FileNotFoundException (systemSettingsPath)); initializedStatus |= SYSTEM_SETTINGS_FAILED; 175 addInitializationFailureMessage(SYSTEM_SETTINGS_FAILED, Messages.getString("PentahoSystem.ERROR_0001_SYSTEM_DIRECTORY_INVALID")); } 177 178 try { 180 systemSettings = new SystemSettings(pApplicationContext); 181 } catch (IllegalArgumentException ex) { 182 initializedStatus |= SYSTEM_PENTAHOXML_FAILED; 185 return false; 186 } 187 188 String auditClass = systemSettings.getSystemSetting("objects/IAuditEntry", "org.pentaho.core.audit.AuditFileEntry"); if (!checkClassExists(auditClass)) { 191 initializedStatus |= SYSTEM_OBJECTS_FAILED | SYSTEM_AUDIT_FAILED; 192 String msg = Messages.getErrorString("PentahoSystem.ERROR_0020_SPECIFIED_CLASS_NOT_FOUND", auditClass); Logger.error(PentahoSystem.class.getName(), msg); 194 addInitializationFailureMessage(SYSTEM_OBJECTS_FAILED, msg); 195 return false; 196 } 197 198 StandaloneSession session = new StandaloneSession("system session"); loggingLevel = Logger.getLogLevel(systemSettings.getSystemSetting("log-level", "ERROR")); Logger.setLogLevel(loggingLevel); 201 202 boolean isOk = true; 203 204 List publisherNodes = systemSettings.getSystemSettings("publishers/*"); if (publisherNodes != null) { 207 Iterator publisherIterator = publisherNodes.iterator(); 208 while (publisherIterator.hasNext()) { 209 String publisherClass = ((Node) publisherIterator.next()).getText(); 210 IPentahoPublisher publisher = (IPentahoPublisher) createObject(publisherClass); 211 if (publisher != null) { 212 publishers.add(publisher); 213 } else { 214 String msg = Messages.getErrorString("PentahoSystem.ERROR_0010_COULD_NOT_CREATE_PUBLISHER", publisherClass); Logger.error(PentahoSystem.class.getName(), msg); 216 initializedStatus |= SYSTEM_PUBLISHERS_FAILED; 217 addInitializationFailureMessage(SYSTEM_PUBLISHERS_FAILED, msg); 218 return false; 219 } 220 } 221 } 222 223 List systemListenerNodes = systemSettings.getSystemSettings("system-listeners/*"); if (systemListenerNodes != null) { 226 Iterator systemListenerIterator = systemListenerNodes.iterator(); 227 isOk = true; 228 while (systemListenerIterator.hasNext()) { 229 Node listenerNode = (Node) systemListenerIterator.next(); 230 String systemListenerClass = listenerNode.getText(); 231 String listenerName = listenerNode.getName(); 232 if( listenerMap != null && listenerMap.get( listenerName ) == null ) { 233 continue; 234 } 235 IPentahoSystemListener listener = (IPentahoSystemListener) createObject(systemListenerClass); 236 if (listener != null) { 237 listeners.add(listener); 238 try { 239 isOk = listener.startup(session); 240 if (!isOk) { 241 String msg = Messages.getString("PentahoSystem.ERROR_0014_STARTUP_FAILURE", systemListenerClass); Logger.error(PentahoSystem.class.getName(), msg); 243 initializedStatus |= SYSTEM_LISTENERS_FAILED; 244 addInitializationFailureMessage(SYSTEM_LISTENERS_FAILED, msg); 245 return false; 246 } 247 } catch (Throwable e) { 248 String msg = Messages.getString("PentahoSystem.ERROR_0014_STARTUP_FAILURE", systemListenerClass); Logger.error(PentahoSystem.class.getName(), msg, e); 250 initializedStatus |= SYSTEM_LISTENERS_FAILED; 251 addInitializationFailureMessage(SYSTEM_LISTENERS_FAILED, msg); 252 return false; 253 } 254 } else { 255 String msg = Messages.getErrorString("PentahoSystem.ERROR_0011_COULD_NOT_CREATE_LISTENER", systemListenerClass); Logger.error(PentahoSystem.class.getName(), msg); 257 initializedStatus |= SYSTEM_LISTENERS_FAILED; 258 addInitializationFailureMessage(SYSTEM_LISTENERS_FAILED, msg); 259 return false; 260 } 261 } 262 } 263 264 List sessionCreateClasses = systemSettings.getSystemSettings("system-actions/*"); if (sessionCreateClasses != null) { 268 Iterator sessionCreateIterator = sessionCreateClasses.iterator(); 269 while (sessionCreateIterator.hasNext()) { 270 Node sessionActionNode = (Node) sessionCreateIterator.next(); 271 272 String sessionCreateClass = sessionActionNode.getName(); 273 String scope = sessionActionNode.selectSingleNode("@scope").getText(); String actionName = sessionActionNode.getText(); 275 List sessionActionList = (List ) sessionCreateMap.get(sessionCreateClass); 276 if (sessionActionList == null) { 277 sessionActionList = new ArrayList (); 278 sessionCreateMap.put(sessionCreateClass, sessionActionList); 279 sessionCreateMap.put("org.pentaho.core.session.UserSession", sessionActionList); } 283 sessionActionList.add(new String [] { scope, actionName }); 284 } 285 } 286 287 return true; 288 } 289 290 public static boolean getInitializedOK() { 291 return initializedStatus == SYSTEM_INITIALIZED_OK; 292 } 293 294 public static int getInitializedStatus() { 295 return initializedStatus; 296 } 297 298 private static List getAdditionalInitializationFailureMessages(int failureBit) { 299 List l = (List )initializationFailureDetailsMap.get(new Integer (failureBit)); 300 return l; 301 } 302 303 public static List getInitializationFailureMessages() { 304 List rtn = new ArrayList (); 305 if (hasFailed(SYSTEM_SETTINGS_FAILED)) { 306 rtn.add(Messages.getString("PentahoSystem.USER_INITIALIZATION_SYSTEM_SETTINGS_FAILED")); List l = getAdditionalInitializationFailureMessages(SYSTEM_SETTINGS_FAILED); 308 if (l!=null) { 309 rtn.addAll(l); 310 } 311 } 312 if (hasFailed(SYSTEM_PUBLISHERS_FAILED)) { 313 rtn.add(Messages.getString("PentahoSystem.USER_INITIALIZATION_SYSTEM_PUBLISHERS_FAILED")); List l = getAdditionalInitializationFailureMessages(SYSTEM_PUBLISHERS_FAILED); 315 if (l!=null) { 316 rtn.addAll(l); 317 } 318 } 319 if (hasFailed(SYSTEM_OBJECTS_FAILED)) { 320 rtn.add(Messages.getString("PentahoSystem.USER_INITIALIZATION_SYSTEM_OBJECTS_FAILED")); List l = getAdditionalInitializationFailureMessages(SYSTEM_OBJECTS_FAILED); 322 if (l!=null) { 323 rtn.addAll(l); 324 } 325 } 326 if (hasFailed(SYSTEM_AUDIT_FAILED)) { 327 rtn.add(Messages.getString("PentahoSystem.USER_INITIALIZATION_SYSTEM_AUDIT_FAILED")); List l = getAdditionalInitializationFailureMessages(SYSTEM_AUDIT_FAILED); 329 if (l!=null) { 330 rtn.addAll(l); 331 } 332 } 333 if (hasFailed(SYSTEM_LISTENERS_FAILED)) { 334 rtn.add(Messages.getString("PentahoSystem.USER_INITIALIZATION_SYSTEM_LISTENERS_FAILED")); List l = getAdditionalInitializationFailureMessages(SYSTEM_LISTENERS_FAILED); 336 if (l!=null) { 337 rtn.addAll(l); 338 } 339 } 340 if (hasFailed(SYSTEM_PENTAHOXML_FAILED)) { 341 rtn.add(Messages.getString("PentahoSystem.USER_INITIALIZATION_SYSTEM_PENTAHOXML_FAILED")); List l = getAdditionalInitializationFailureMessages(SYSTEM_PENTAHOXML_FAILED); 343 if (l!=null) { 344 rtn.addAll(l); 345 } 346 } 347 return rtn; 348 } 349 350 public static synchronized void addInitializationFailureMessage(int failureBit, String message) { 351 Integer i = new Integer (failureBit); 352 List l = (List )initializationFailureDetailsMap.get(i); 353 if (l == null) { 354 l=new ArrayList (); 355 initializationFailureDetailsMap.put(i, l); 356 } 357 l.add(" " + message); } 359 360 private static final boolean hasFailed(int errorToCheck) { 361 return ( (initializedStatus & errorToCheck) == errorToCheck); 362 } 363 364 protected static boolean checkClassExists(String className) { 365 try { 366 Class.forName(className); 367 return true; 368 } catch (Throwable t) { } 369 return false; 370 } 371 372 public static boolean loadStartupObject(IPentahoSession session, String objectName, boolean required ) { 373 String scope; 374 String objectClass = PentahoSystem.getSystemSetting( "objects/"+objectName, null ); scope = PentahoSystem.getSystemSetting( "objects/"+objectName+"/@scope", null ); if( debug ) Logger.debug( PentahoSystem.class.getName(), Messages.getString("PentahoSystem.DEBUG_CLASS_INFO",objectName,objectClass,scope )); if( objectClass == null ) { 378 if (required) { 379 Logger.error( PentahoSystem.class.getName(), Messages.getErrorString("PentahoSystem.ERROR_0021_OBJECT_NOT_SPECIFIED", objectName) ); return false; 381 } else { 382 Logger.warn( PentahoSystem.class.getName(), Messages.getString("PentahoSystem.ERROR_0021_OBJECT_NOT_SPECIFIED", objectName) ); return true; 384 } 385 } 386 if( scope == null ) { 387 Logger.warn( PentahoSystem.class.getName(), Messages.getString("PentahoSystem.WARNING_OBJECT_SCOPE_MISSING", objectName) ); scope = SCOPE_SESSION; 389 } 390 classNameMap.put( objectName, objectClass ); 391 scopeMap.put( objectName, scope ); 392 if( SCOPE_GLOBAL.equals( scope ) ) { 393 try { 394 Object obj = PentahoSystem.createObject( objectClass ); 395 if( obj == null ) { 396 Logger.error( PentahoSystem.class.getName(), Messages.getErrorString("PentahoSystem.ERROR_0022_COULD_NOT_CREATE_OBJECT", objectName) ); return false; 398 } else { 399 globalObjectsMap.put(objectName, obj); 400 } 401 if (obj instanceof IPentahoInitializer) { 402 ((IPentahoInitializer)obj).init( session ); 403 } 404 405 } catch ( ClassCastException e ) { 406 e.printStackTrace(); 408 } catch ( Throwable t ) { 409 t.printStackTrace(); 411 412 } 413 } 414 415 return true; 416 } 417 418 public static IUITemplater getUITemplater( IPentahoSession session ) { 419 Object obj = getObject( session, "IUITemplater" ); return (IUITemplater) obj; 421 } 422 423 public static IBackgroundExecution getBackgroundExecutionHandler( IPentahoSession session ) { 424 Object obj = getObject( session, BACKGROUND_EXECUTION ); 425 return (IBackgroundExecution)obj; 426 } 427 428 public static IXMLComponent getUserFilesComponent( IPentahoSession session ) { 429 Object obj = getObject( session, "IUserFilesComponent" ); return (IXMLComponent) obj; 431 } 432 433 public static Object getObject( IPentahoSession session, String objectName ) { 434 String scope = (String ) scopeMap.get( objectName ); 440 441 if( SCOPE_GLOBAL.equalsIgnoreCase( scope ) ) { 442 Object rtn = globalObjectsMap.get(objectName); 443 if (rtn instanceof IPentahoInitializer) { 444 ((IPentahoInitializer)rtn).init(session); 445 } 446 return rtn; 447 } else if( SCOPE_SESSION.equalsIgnoreCase( scope ) ) { 448 Object attribute = session.getAttribute( objectName ); 449 450 if( (attribute != null) ) { 451 if (attribute instanceof IPentahoInitializer) { 453 ((IPentahoInitializer)attribute).init( session ); 454 } 455 return attribute; 456 } 457 String clazz = (String ) classNameMap.get( objectName ); 458 attribute = PentahoSystem.createObject( clazz ); 459 if (attribute instanceof IPentahoInitializer) { 460 ((IPentahoInitializer)attribute).init( session ); 461 } 462 session.setAttribute( objectName, attribute ); 463 return attribute; 464 } 465 Logger.error( PentahoSystem.class.getName(), Messages.getErrorString("PentahoSystem.ERROR_0023_COULD_NOT_RETURN_OBJECT", objectName, scope) ); return null; 467 468 } 469 470 protected static boolean initGlobalObjects(IPentahoSession session) { 471 String solutionEngineClass = systemSettings.getSystemSetting("objects/ISolutionEngine", null); String scope = systemSettings.getSystemSetting("objects/ISolutionEngine/@scope", null); if (debug) 474 Logger.debug(PentahoSystem.class.getName(), Messages.getString("PentahoSystem.DEBUG_SOLUTION_ENGINE_CLASS", solutionEngineClass, scope)); if (solutionEngineClass == null) { 476 String msg = Messages.getErrorString("PentahoSystem.ERROR_0002_SOLUTION_ENGINE_NOT_SPECIFIED"); Logger.error(PentahoSystem.class.getName(), msg); 478 initializedStatus |= SYSTEM_OBJECTS_FAILED; 479 addInitializationFailureMessage(SYSTEM_OBJECTS_FAILED, msg); 480 return false; 481 } 482 if (scope == null) { 483 Logger.warn(PentahoSystem.class.getName(), Messages.getString("PentahoSystem.WARNING_SOLUTION_ENGINE_SCOPE_MISSING")); scope = SCOPE_REQUEST; 485 } 486 classNameMap.put(SOLUTION_ENGINE, solutionEngineClass); 487 scopeMap.put(SOLUTION_ENGINE, scope); 488 if (SCOPE_GLOBAL.equals(scope)) { 489 ISolutionEngine obj = (ISolutionEngine) createObject(solutionEngineClass); 490 if (obj == null) { 491 String msg = Messages.getErrorString("PentahoSystem.ERROR_0003_COULD_NOT_CREATE_SOLUTION_ENGINE"); Logger.error(PentahoSystem.class.getName(), msg); 493 initializedStatus |= SYSTEM_OBJECTS_FAILED; 494 addInitializationFailureMessage(SYSTEM_OBJECTS_FAILED, msg); 495 return false; 496 } else { 497 globalObjectsMap.put(SOLUTION_ENGINE, obj); 498 } 499 } else { 500 if (!checkClassExists(solutionEngineClass)) { 501 String msg = Messages.getErrorString("PentahoSystem.ERROR_0020_SPECIFIED_CLASS_NOT_FOUND", solutionEngineClass); Logger.error(PentahoSystem.class.getName(), msg); 503 initializedStatus |= SYSTEM_OBJECTS_FAILED; 504 addInitializationFailureMessage(SYSTEM_OBJECTS_FAILED, msg); 505 return false; 506 } 507 } 508 509 String solutionRepositoryClass = systemSettings.getSystemSetting("objects/ISolutionRepository", null); scope = systemSettings.getSystemSetting("objects/ISolutionRepository/@scope", null); if (debug) 512 Logger.debug(PentahoSystem.class.getName(), Messages.getString("PentahoSystem.DEBUG_SOLUTION_REPOSITORY_CLASS", solutionRepositoryClass, scope)); if (solutionRepositoryClass == null) { 514 String msg = Messages.getErrorString("PentahoSystem.ERROR_0004_SOLUTION_REPOSITORY_NOT_SPECIFIED"); Logger.error(PentahoSystem.class.getName(), msg); 516 initializedStatus |= SYSTEM_OBJECTS_FAILED; 517 addInitializationFailureMessage(SYSTEM_OBJECTS_FAILED, msg); 518 return false; 519 } 520 if (scope == null) { 521 Logger.warn(PentahoSystem.class.getName(), Messages.getString("PentahoSystem.WARNING_SOLUTION_REPOSITORY_SCOPE_MISSING")); scope = SCOPE_SESSION; 523 } 524 classNameMap.put(SOLUTION_REPOSITORY, solutionRepositoryClass); 525 scopeMap.put(SOLUTION_REPOSITORY, scope); 526 if (SCOPE_GLOBAL.equals(scope)) { 527 ISolutionRepository obj = (ISolutionRepository) createObject(solutionRepositoryClass); 528 if (obj == null) { 529 String msg = Messages.getErrorString("PentahoSystem.ERROR_0005_COULD_NOT_CREATE_SOLUTION_REPOSITORY"); Logger.error(PentahoSystem.class.getName(), msg); 531 initializedStatus |= SYSTEM_OBJECTS_FAILED; 532 addInitializationFailureMessage(SYSTEM_OBJECTS_FAILED, msg); 533 return false; 534 } else { 535 globalObjectsMap.put(SOLUTION_REPOSITORY, obj); 536 } 537 obj.init(session); 538 obj.setLoggingLevel(loggingLevel); 539 } else { 540 if (!checkClassExists(solutionRepositoryClass)) { 541 String msg = Messages.getErrorString("PentahoSystem.ERROR_0020_SPECIFIED_CLASS_NOT_FOUND", solutionRepositoryClass); Logger.error(PentahoSystem.class.getName(), msg); 543 initializedStatus |= SYSTEM_OBJECTS_FAILED; 544 addInitializationFailureMessage(SYSTEM_OBJECTS_FAILED, msg); 545 return false; 546 } 547 } 548 549 550 String runtimeRepositoryClass = systemSettings.getSystemSetting("objects/IRuntimeRepository", null); scope = systemSettings.getSystemSetting("objects/IRuntimeRepository/@scope", null); if (debug) 553 Logger.debug(PentahoSystem.class.getName(), Messages.getString("PentahoSystem.DEBUG_RUNTIME_REPOSITORY_CLASS", runtimeRepositoryClass, scope)); if (runtimeRepositoryClass == null) { 555 String msg = Messages.getErrorString("PentahoSystem.ERROR_0006_RUNTIME_REPOSITORY_NOT_SPECIFIED"); Logger.error(PentahoSystem.class.getName(), msg); 557 initializedStatus |= SYSTEM_OBJECTS_FAILED; 558 addInitializationFailureMessage(SYSTEM_OBJECTS_FAILED, msg); 559 return false; 560 } 561 562 if (scope == null) { 563 Logger.warn(PentahoSystem.class.getName(), Messages.getString("PentahoSystem.WARNING_RUNTIME_REPOSITORY_SCOPE_MISSING")); scope = SCOPE_SESSION; 565 } 566 567 classNameMap.put(RUNTIME_REPOSITORY, runtimeRepositoryClass); 568 scopeMap.put(RUNTIME_REPOSITORY, scope); 569 if (SCOPE_GLOBAL.equals(scope)) { 570 IRuntimeRepository obj = (IRuntimeRepository) createObject(runtimeRepositoryClass); 571 if (obj == null) { 572 String msg = Messages.getErrorString("PentahoSystem.ERROR_0007_COULD_NOT_CREATE_RUNTIME_REPOSITORY"); Logger.error(PentahoSystem.class.getName(), msg); 574 initializedStatus |= SYSTEM_OBJECTS_FAILED; 575 addInitializationFailureMessage(SYSTEM_OBJECTS_FAILED, msg); 576 return false; 577 } else { 578 globalObjectsMap.put(RUNTIME_REPOSITORY, obj); 579 obj.setSession(session); 580 runtimeRepository = obj; 581 } 582 } else { 583 if (!checkClassExists(runtimeRepositoryClass)) { 584 String msg = Messages.getErrorString("PentahoSystem.ERROR_0020_SPECIFIED_CLASS_NOT_FOUND", runtimeRepositoryClass); initializedStatus |= SYSTEM_OBJECTS_FAILED; 586 Logger.error(PentahoSystem.class.getName(), msg); 587 addInitializationFailureMessage(SYSTEM_OBJECTS_FAILED, msg); 588 return false; 589 } 590 } 591 592 String contentRepositoryClass = systemSettings.getSystemSetting("objects/IContentRepository", null); scope = systemSettings.getSystemSetting("objects/IContentRepository/@scope", null); if (debug) 595 Logger.debug(PentahoSystem.class.getName(), Messages.getString("PentahoSystem.DEBUG_CONTENT_REPOSITORY_CLASS", contentRepositoryClass, scope)); if (contentRepositoryClass == null) { 597 Logger.warn(PentahoSystem.class.getName(), Messages.getErrorString("PentahoSystem.ERROR_0008_CONTENT_REPOSITORY_NOT_SPECIFIED")); } else { 599 if (scope == null) { 600 Logger.warn(PentahoSystem.class.getName(), Messages.getString("PentahoSystem.WARNING_CONTENT_REPOSITORY_SCOPE_MISSING")); scope = SCOPE_SESSION; 602 } 603 classNameMap.put(CONTENT_REPOSITORY, contentRepositoryClass); 604 scopeMap.put(CONTENT_REPOSITORY, scope); 605 if (SCOPE_GLOBAL.equals(scope)) { 606 IContentRepository obj = (IContentRepository) createObject(contentRepositoryClass); 607 if (obj == null) { 608 String msg = Messages.getErrorString("PentahoSystem.ERROR_0009_COULD_NOT_CREATE_CONTENT_REPOSITORY"); Logger.error(PentahoSystem.class.getName(), msg); 610 initializedStatus |= SYSTEM_OBJECTS_FAILED; 611 addInitializationFailureMessage(SYSTEM_OBJECTS_FAILED, msg); 612 return false; 613 } 614 globalObjectsMap.put(CONTENT_REPOSITORY, obj); 615 obj.setSession(session); 616 if( contentRepository == null ) { 617 contentRepository = obj; 618 } 619 620 } else { 621 if (!checkClassExists(contentRepositoryClass)) { 622 String msg = Messages.getErrorString("PentahoSystem.ERROR_0020_SPECIFIED_CLASS_NOT_FOUND", contentRepositoryClass); initializedStatus |= SYSTEM_OBJECTS_FAILED; 624 Logger.error(PentahoSystem.class.getName(), msg); 625 addInitializationFailureMessage(SYSTEM_OBJECTS_FAILED, msg); 626 return false; 627 } 628 } 629 } 630 loadStartupObject( session, "IUITemplater", false ); loadStartupObject( session, "IUserFilesComponent", false ); loadStartupObject( session, BACKGROUND_EXECUTION, false); 633 sessionStartup(session, false); 634 return true; 635 } 636 637 public static String getSystemName() { 638 return Messages.getString("PentahoSystem.USER_SYSTEM_TITLE"); } 640 641 public static List getSessionCreateActions(String className) { 642 List sessionCreateActions = (List ) sessionCreateMap.get(className); 643 return sessionCreateActions; 644 } 645 646 public static IParameterProvider getGlobalParameters() { 647 return globalParameters; 648 } 649 650 public static void sessionStartup(IPentahoSession session) { 651 sessionStartup(session, true); 652 } 653 654 public static void clearGlobals() { 655 globalAttributes.clear(); 656 } 657 658 public static void sessionStartup(IPentahoSession session, boolean doSession) { 659 List actions = getSessionCreateActions(session.getClass().getName()); 660 if (actions == null) { 661 return; 663 } 664 665 if (!session.isAuthenticated() && doSession) { 666 return; 667 } 668 boolean doGlobals = globalAttributes.size() == 0; 669 Iterator actionsIterator = actions.iterator(); 671 while (actionsIterator.hasNext()) { 672 String tmp[] = (String []) actionsIterator.next(); 673 String scope = tmp[0]; 674 if ("global".equals(scope) && !doGlobals) { continue; 677 } else if ("session".equals(scope) && !doSession) { continue; 679 } 680 String actionStr = tmp[1]; 681 String actionInfo[] = parseActionString(actionStr); 683 if (actionInfo != null && actionInfo.length == 3) { 684 String solutionId = actionInfo[0]; 685 String path = actionInfo[1]; 686 String actionName = actionInfo[2]; 687 689 SimpleOutputHandler outputHandler = null; 690 691 String instanceId = null; 692 693 ISolutionEngine solutionEngine = PentahoSystem.getSolutionEngineInstance(session); 694 solutionEngine.setLoggingLevel(loggingLevel); 695 solutionEngine.init(session); 696 697 String baseUrl = ""; HashMap parameterProviderMap = new HashMap (); 699 HttpSessionParameterProvider sessionParameters = new HttpSessionParameterProvider(session); 700 parameterProviderMap.put("session", sessionParameters); 702 IPentahoUrlFactory urlFactory = new SimpleUrlFactory(baseUrl); 703 704 ArrayList messages = new ArrayList (); 705 706 IRuntimeContext context = null; 707 try { 708 context = solutionEngine.execute(solutionId, path, actionName, "Session startup actions", false, true, instanceId, false, parameterProviderMap, outputHandler, null, urlFactory, messages); 710 if (context.getStatus() == IRuntimeContext.RUNTIME_STATUS_SUCCESS) { 711 Iterator outputIterator = context.getOutputNames().iterator(); 713 while (outputIterator.hasNext()) { 714 IActionParameter output = context.getOutputParameter((String ) outputIterator.next()); 715 String attributeName = output.getName(); 716 Object data = output.getValue(); 717 if (data != null) { 718 if ("session".equals(scope)) { session.removeAttribute(attributeName); 720 session.setAttribute(attributeName, data); 721 } else if ("global".equals(scope)) { globalAttributes.remove(attributeName); 723 globalAttributes.put(attributeName, data); 724 } 725 726 } 727 } 728 } 729 } finally { 730 if (context != null) { 731 context.dispose(); 732 } 733 } 734 735 } else { 736 Logger.error(PentahoSystem.class.getName(), Messages.getString("PentahoSystem.ERROR_0016_COULD_NOT_PARSE_ACTION", actionStr)); } 738 739 } 740 } 741 742 public static String [] parseActionString(String actionString) { 743 if (actionString == null) { 746 return null; 747 } 748 749 actionString = actionString.replace('\\', '/'); 751 752 if (actionString.charAt(0) == '/') { 754 actionString = actionString.substring(1); 755 } 756 757 String solution; 758 String path = ""; String name; 760 int idx1 = actionString.indexOf('/'); 761 int idx2 = actionString.lastIndexOf('/'); 762 if (idx1 == -1) { 763 return null; 765 } 766 solution = actionString.substring(0, idx1); 767 name = actionString.substring(idx2 + 1); 768 if (idx2 > idx1) { 769 path = actionString.substring(idx1 + 1, idx2); 770 } 771 return new String [] { solution, path, name }; 772 } 773 774 public static void shutdown() { 775 if (LocaleHelper.getLocale() == null) { 776 LocaleHelper.setLocale(Locale.getDefault()); 777 } 778 Iterator systemListenerIterator = listeners.iterator(); 779 while (systemListenerIterator.hasNext()) { 780 IPentahoSystemListener listener = (IPentahoSystemListener) systemListenerIterator.next(); 781 if (listener != null) { 782 try { 783 listener.shutdown(); 784 } catch (Throwable e) { 785 Logger.error(PentahoSystem.class.getName(), Messages.getString("PentahoSystem.ERROR_0015_SHUTDOWN_FAILURE") + listener.getClass().getName(), e); } 787 } 788 } 789 } 790 791 public static IApplicationContext getApplicationContext() { 792 return applicationContext; 793 } 794 795 public static ISolutionEngine getSolutionEngineInstance(IPentahoSession session) { 796 String scope = (String ) scopeMap.get(SOLUTION_ENGINE); 798 if (SCOPE_GLOBAL.equalsIgnoreCase(scope)) { 799 return (ISolutionEngine) globalObjectsMap.get(SOLUTION_ENGINE); 800 } 801 else if (SCOPE_SESSION.equalsIgnoreCase(scope)) { 802 if( session == null ) { 803 Logger.error(PentahoSystem.class.getName(), Messages.getErrorString("PentahoSystem.ERROR_0003_COULD_NOT_CREATE_SOLUTION_ENGINE")); return null; 805 } 806 Object attribute = session.getAttribute(SOLUTION_ENGINE); 807 if ((attribute != null) && (attribute instanceof ISolutionEngine)) { 808 ISolutionEngine solutionEngine = (ISolutionEngine) attribute; 810 return solutionEngine; 811 } 812 String solutionEngineClass = (String ) classNameMap.get(SOLUTION_ENGINE); 813 Object obj = createObject(solutionEngineClass); 814 if ( obj == null || !(obj instanceof ISolutionEngine ) ) { 815 Logger.error(PentahoSystem.class.getName(), Messages.getErrorString("PentahoSystem.ERROR_0003_COULD_NOT_CREATE_SOLUTION_ENGINE")); return null; 817 } 818 ISolutionEngine solutionEngine = (ISolutionEngine) obj; 819 session.setAttribute(SOLUTION_ENGINE, solutionEngine); 820 return solutionEngine; 821 } 822 else if (SCOPE_REQUEST.equalsIgnoreCase(scope)) { 823 if( session == null ) { 824 Logger.error(PentahoSystem.class.getName(), Messages.getErrorString("PentahoSystem.ERROR_0003_COULD_NOT_CREATE_SOLUTION_ENGINE")); return null; 826 } 827 String solutionEngineClass = (String ) classNameMap.get(SOLUTION_ENGINE); 828 Object obj = createObject(solutionEngineClass); 829 if ( obj == null || !(obj instanceof ISolutionEngine ) ) { 830 Logger.error(PentahoSystem.class.getName(), Messages.getErrorString("PentahoSystem.ERROR_0003_COULD_NOT_CREATE_SOLUTION_ENGINE")); return null; 832 } 833 ISolutionEngine solutionEngine = (ISolutionEngine) obj; 834 return solutionEngine; 835 } 836 837 return null; 838 } 839 840 public static IContentRepository getContentRepository(IPentahoSession session) { 841 String scope = (String ) scopeMap.get(CONTENT_REPOSITORY); 842 if (SCOPE_GLOBAL.equalsIgnoreCase(scope)) { 843 return (IContentRepository) globalObjectsMap.get(CONTENT_REPOSITORY); 844 } else if (SCOPE_SESSION.equalsIgnoreCase(scope)) { 845 if( session == null ) { 846 Logger.error(PentahoSystem.class.getName(), Messages.getErrorString("PentahoSystem.ERROR_0009_COULD_NOT_CREATE_CONTENT_REPOSITORY")); return null; 848 } 849 Object attribute = null; 850 try { 851 attribute = session.getAttribute(CONTENT_REPOSITORY); 852 } catch (Throwable t) { 853 session.error(Messages.getErrorString("PentahoSystem.ERROR_0012_COULD_NOT_GET_CONTENT_REPOSITORY")); } 855 if ((attribute != null) && (attribute instanceof IContentRepository)) { 856 IContentRepository repository = (IContentRepository) attribute; 858 repository.setSession(session); 859 if( contentRepository == null ) { 860 contentRepository = repository; 861 } 862 return repository; 863 } 864 String contentRepositoryClass = (String ) classNameMap.get(CONTENT_REPOSITORY); 865 Object obj = createObject(contentRepositoryClass); 866 if ( obj == null || !(obj instanceof IContentRepository ) ) { 867 Logger.error(PentahoSystem.class.getName(), Messages.getErrorString("PentahoSystem.ERROR_0009_COULD_NOT_CREATE_CONTENT_REPOSITORY")); return null; 869 } 870 IContentRepository repository = (IContentRepository) obj; 871 repository.setSession(session); 872 session.setAttribute(CONTENT_REPOSITORY, repository); 873 if( contentRepository == null ) { 874 contentRepository = repository; 875 } 876 return repository; 877 } 878 return null; 879 } 880 881 public static ISolutionRepository getSolutionRepository(IPentahoSession session) { 882 String scope = (String ) scopeMap.get(SOLUTION_REPOSITORY); 883 884 886 if (SCOPE_GLOBAL.equalsIgnoreCase(scope)) { 887 return (ISolutionRepository) globalObjectsMap.get(SOLUTION_REPOSITORY); 888 } else if (SCOPE_SESSION.equalsIgnoreCase(scope)) { 889 if( session == null ) { 890 Logger.error(PentahoSystem.class.getName(), Messages.getErrorString("PentahoSystem.ERROR_0005_COULD_NOT_CREATE_SOLUTION_REPOSITORY")); return null; 892 } 893 Object attribute = session.getAttribute(SOLUTION_REPOSITORY); 894 if ((attribute != null) && (attribute instanceof ISolutionRepository)) { 895 ISolutionRepository aSolutionRepository = (ISolutionRepository) attribute; 897 aSolutionRepository.setLocale(LocaleHelper.getLocale()); 898 aSolutionRepository.init(session); 899 aSolutionRepository.setLoggingLevel(loggingLevel); 900 return aSolutionRepository; 901 } 902 String solutionRepositoryClass = (String ) classNameMap.get(SOLUTION_REPOSITORY); 903 Object obj = createObject(solutionRepositoryClass); 904 if ( obj == null || !(obj instanceof ISolutionRepository ) ) { 905 Logger.error(PentahoSystem.class.getName(), Messages.getErrorString("PentahoSystem.ERROR_0005_COULD_NOT_CREATE_SOLUTION_REPOSITORY")); return null; 907 } 908 ISolutionRepository aSolutionRepository = (ISolutionRepository) obj; 909 aSolutionRepository.init(session); 910 aSolutionRepository.setLoggingLevel(loggingLevel); 911 session.setAttribute(SOLUTION_REPOSITORY, aSolutionRepository); 912 return aSolutionRepository; 913 } 914 return null; 915 } 916 917 public static IRuntimeRepository getRuntimeRepository(IPentahoSession session) { 918 String scope = (String ) scopeMap.get(RUNTIME_REPOSITORY); 919 if (SCOPE_GLOBAL.equalsIgnoreCase(scope)) { 920 return (IRuntimeRepository) globalObjectsMap.get(RUNTIME_REPOSITORY); 921 } 922 else if (SCOPE_SESSION.equalsIgnoreCase(scope)) { 923 if( session == null ) { 924 Logger.error(PentahoSystem.class.getName(), Messages.getErrorString("PentahoSystem.ERROR_0007_COULD_NOT_CREATE_RUNTIME_REPOSITORY")); return null; 926 } 927 Object attribute = session.getAttribute(RUNTIME_REPOSITORY); 928 if ((attribute != null) && (attribute instanceof IRuntimeRepository)) { 929 IRuntimeRepository repository = (IRuntimeRepository) attribute; 931 repository.setSession(session); 932 if( runtimeRepository == null ) { 933 runtimeRepository = repository; 934 } 935 return repository; 936 } 937 String runtimeRepositoryClass = (String ) classNameMap.get(RUNTIME_REPOSITORY); 938 Object obj = createObject(runtimeRepositoryClass); 939 if ( obj == null || !(obj instanceof IRuntimeRepository ) ) { 940 Logger.error(PentahoSystem.class.getName(), Messages.getErrorString("PentahoSystem.ERROR_0007_COULD_NOT_CREATE_RUNTIME_REPOSITORY")); return null; 942 } 943 IRuntimeRepository repository = (IRuntimeRepository) obj; 944 repository.setSession(session); 945 session.setAttribute(RUNTIME_REPOSITORY, repository); 946 if( runtimeRepository == null ) { 947 runtimeRepository = repository; 948 } 949 return repository; 950 } 951 return null; 952 } 953 954 public static Object createObject(String className, ILogger logger) { 955 956 Object object = null; 957 try { 958 959 Class componentClass = Class.forName(className); 960 object = componentClass.newInstance(); 961 962 } catch (Throwable t) { 963 logger.fatal(Messages.getErrorString("PentahoSystem.ERROR_0013_COULD_NOT_CREATE_OBEJCT", className), t); } 965 return object; 966 } 967 968 public static Object createObject(String className) { 969 970 Object object = null; 971 try { 972 973 Class componentClass = Class.forName(className); 974 object = componentClass.newInstance(); 975 976 } catch (Throwable t) { 977 Logger.fatal(PentahoSystem.class.getName(), Messages.getErrorString("PentahoSystem.ERROR_0013_COULD_NOT_CREATE_OBEJCT", className), t); } 979 return object; 980 } 981 982 public static String getSystemSetting(String path, String settingName, String defaultValue) { 983 return systemSettings.getSystemSetting(path, settingName, defaultValue); 984 } 985 986 public static String getSystemSetting(String settingName, String defaultValue) { 987 return systemSettings.getSystemSetting(settingName, defaultValue); 989 } 990 991 public static ISystemSettings getSystemSettings() { 992 return systemSettings; 993 } 994 995 public static void refreshSettings() { 996 systemSettings.resetSettingsCache(); 997 } 998 999 public static String publish(IPentahoSession session, String className) { 1000 Iterator publisherIterator = publishers.iterator(); 1001 while (publisherIterator.hasNext()) { 1003 IPentahoPublisher publisher = (IPentahoPublisher) publisherIterator.next(); 1004 if (publisher != null && (className == null || className.equals(publisher.getClass().getName()))) { 1005 try { 1006 return publisher.publish(session, loggingLevel); 1007 } catch (Throwable e) { 1008 1009 } 1010 } 1011 } 1012 return Messages.getString("PentahoSystem.ERROR_0017_PUBLISHER_NOT_FOUND"); } 1014 1015 public static List getPublisherList() { 1016 return publishers; 1017 } 1018 1019 public static Document getPublishersDocument() { 1020 1021 Document document = DocumentHelper.createDocument(); 1022 Element root = document.addElement("publishers"); 1024 Iterator publisherIterator = publishers.iterator(); 1025 while (publisherIterator.hasNext()) { 1028 IPentahoPublisher publisher = (IPentahoPublisher) publisherIterator.next(); 1029 if (publisher != null) { 1030 try { 1031 Element publisherNode = root.addElement("publisher"); publisherNode.addElement("name").setText(publisher.getName()); publisherNode.addElement("description").setText(publisher.getDescription()); publisherNode.addElement("class").setText(publisher.getClass().getName()); 1036 } catch (Throwable e) { 1037 1038 } 1039 } 1040 } 1041 1042 return document; 1043 1044 } 1045 1046 public static void systemEntryPoint() { 1047 applicationContext.invokeEntryPoints(); 1048 } 1049 1050 public static void systemExitPoint() { 1051 applicationContext.invokeExitPoints(); 1052 } 1053 1054}
| Popular Tags
|