001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: */
019:
020: package de.schlund.pfixxml;
021:
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Properties;
025: import java.util.StringTokenizer;
026:
027: import org.w3c.dom.Document;
028: import org.w3c.dom.Node;
029:
030: import de.schlund.pfixxml.resources.FileResource;
031: import de.schlund.pfixxml.resources.ResourceUtil;
032: import de.schlund.pfixxml.targets.TargetGeneratorFactory;
033: import de.schlund.pfixxml.util.XPath;
034: import de.schlund.pfixxml.util.Xml;
035:
036: /**
037: * Initializes the TargetGenerators of the projects on server startup.
038: * This is only useful in production mode, when the editor factory is
039: * deactivated and thus does not do this job.
040: * Set the property "projectinit.projectlist" to specify a comma-seperated
041: * list of all projects which should be initialized on startup.
042: * If no such property is specified, all known projects are initialized.
043: *
044: * @author Sebastian Marsching <sebastian.marsching@1und1.de>
045: */
046: public class ProjectInitFactory {
047: private final static String PROJECTS_XML = "servletconf/projects.xml";
048:
049: private static ProjectInitFactory instance = new ProjectInitFactory();
050:
051: public static ProjectInitFactory getInstance() {
052: return instance;
053: }
054:
055: public void init(Properties props) throws Exception {
056: FileResource projectsFile = ResourceUtil
057: .getFileResourceFromDocroot(PROJECTS_XML);
058: Document doc = Xml.parseMutable(projectsFile);
059:
060: String projectlist = props
061: .getProperty("projectinit.projectlist");
062: if (projectlist == null) {
063: // Get a list of all TargetGenerators used by any project
064: List<Node> dependNodes = XPath.select(doc
065: .getDocumentElement(), "project/depend/text()");
066: for (Iterator<Node> i = dependNodes.iterator(); i.hasNext();) {
067: Node dependNode = i.next();
068: String dependPath = dependNode.getNodeValue();
069: // Create TargetGenerator to make sure it is cached
070: // by TargetGeneratorFactory
071: TargetGeneratorFactory
072: .getInstance()
073: .createGenerator(
074: ResourceUtil
075: .getFileResourceFromDocroot(dependPath));
076: }
077: } else {
078: // Get TargetGenerator for each project specified
079: StringTokenizer tokenizer = new StringTokenizer(
080: projectlist, ",");
081: while (tokenizer.hasMoreTokens()) {
082: String projectName = tokenizer.nextToken().trim();
083: if (projectName.equals("")) {
084: continue;
085: }
086: Node dependNode = XPath.selectNode(doc
087: .getDocumentElement(), "project[@name='"
088: + projectName + "']/depend/text()");
089: if (dependNode == null) {
090: continue;
091: }
092: String dependPath = dependNode.getNodeValue();
093: if (dependPath == null || dependPath.equals("")) {
094: continue;
095: }
096: // Create TargetGenerator to make sure it is cached
097: // by TargetGeneratorFactory
098: TargetGeneratorFactory
099: .getInstance()
100: .createGenerator(
101: ResourceUtil
102: .getFileResourceFromDocroot(dependPath));
103: }
104: }
105: }
106: }
|