001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.core;
019:
020: import java.util.Collection;
021: import java.util.Iterator;
022:
023: import de.finix.contelligent.CallData;
024: import de.finix.contelligent.Component;
025: import de.finix.contelligent.ComponentManager;
026: import de.finix.contelligent.ComponentNotFoundException;
027: import de.finix.contelligent.ComponentPath;
028: import de.finix.contelligent.Container;
029: import de.finix.contelligent.Contelligent;
030: import de.finix.contelligent.Session;
031: import de.finix.contelligent.core.security.ContelligentSecurityManager;
032: import de.finix.contelligent.logging.LoggingService;
033:
034: /**
035: * A thread that starts loading components from the database to the component
036: * cache after the main server startup is done.
037: */
038: public final class AutoLoader implements Runnable {
039: final static org.apache.log4j.Logger log = LoggingService
040: .getLogger(AutoLoader.class);
041:
042: private Thread thread;
043:
044: private Contelligent contelligent;
045:
046: private Collection roots;
047:
048: private int totalLoaded;
049:
050: private int mySize;
051:
052: public AutoLoader(Contelligent contelligent, Collection roots) {
053: this .contelligent = contelligent;
054: this .roots = roots;
055: thread = ThreadFactory.getInstance().createThread("AutoLoader",
056: this );
057: thread.setDaemon(true);
058: thread.setPriority(Thread.MIN_PRIORITY);
059: thread.start();
060: }
061:
062: private void loadTree(ComponentPath root, CallData callData,
063: ComponentManager cm) {
064: try {
065: Component c = cm.getComponent(root, callData, false);
066: totalLoaded++;
067: if ((totalLoaded % 1000) == 0) {
068: log.info("Autoload status: " + totalLoaded + "/"
069: + mySize);
070: }
071: if (c instanceof Container) {
072: Iterator names = cm.getSubcomponentNames((Container) c)
073: .iterator();
074: while (names.hasNext()) {
075: String name = (String) names.next();
076: ComponentPath newPath = root.append(name);
077: loadTree(newPath, callData, cm);
078: }
079: }
080: } catch (ComponentNotFoundException cne) {
081: log.info("Component [" + root.toPath()
082: + "] not found. Might have been deleted.");
083: }
084: }
085:
086: public void run() {
087: log.info("Thread started.");
088: totalLoaded = 0;
089: mySize = 0;
090: try {
091: ComponentManagerInternal cm = (ComponentManagerInternal) contelligent
092: .getRootComponentManager();
093: Session session = contelligent.beginSession(
094: ContelligentSecurityManager.getInstance()
095: .getGuest(), cm);
096: contelligent.beginTx(3600);
097: CallData callData = contelligent.createCallData(session);
098: Iterator rootIterator = roots.iterator();
099: while (rootIterator.hasNext()) {
100: ComponentPath rootCp = (ComponentPath) rootIterator
101: .next();
102: try {
103: Component rootComp = cm.getComponent(rootCp,
104: callData, false);
105: mySize += cm.getSubtreeComponents(rootComp);
106: } catch (ComponentNotFoundException cne) {
107: // Silently ignore this, since a tree that is not there
108: // does not take up any space in the cache either.
109: }
110: }
111: int maxSize = cm.getMaxCacheSize();
112: log.info("Going to preload approximately " + mySize
113: + " components.");
114: if (mySize > maxSize) {
115: log
116: .warn("Components to be preloaded ("
117: + mySize
118: + ") exceed maximum cache size ("
119: + maxSize
120: + ")! Please adjust your configuration accordingly.");
121: }
122: // reset iterator
123: rootIterator = roots.iterator();
124: while (rootIterator.hasNext()) {
125: ComponentPath rootCp = (ComponentPath) rootIterator
126: .next();
127: log.info("Loading: " + rootCp.toPath());
128: loadTree(rootCp, callData, cm);
129: }
130: log.info("All done. Going to commit.");
131: contelligent.commitTx();
132: } catch (Exception e) {
133: log.info("An exception occured while autoloading.", e);
134: try {
135: contelligent.rollbackTx();
136: } catch (javax.transaction.SystemException se) {
137: log.error("Rollback failed.", se);
138: }
139: }
140: log.info("Thread successfully terminated.");
141: }
142:
143: public void stop() {
144: thread.interrupt();
145: }
146: }
|