Author: prasad Date: Tue Dec 4 09:03:59 2007 New Revision: 601001 URL: http://svn.apache.org/viewvc?rev=601001&view=rev Log: * parseFiles() and cleanup() Modified: geronimo/sandbox/jetspeed-integration/jetspeed-geronimo/src/main/java/org/apache/geronimo/jetspeed/integration/JetspeedSerializerGBean.java Modified: geronimo/sandbox/jetspeed-integration/jetspeed-geronimo/src/main/java/org/apache/geronimo/jetspeed/integration/JetspeedSerializerGBean.java URL: http://svn.apache.org/viewvc/geronimo/sandbox/jetspeed-integration/jetspeed-geronimo/src/main/java/org/apache/geronimo/jetspeed/integration/JetspeedSerializerGBean.java?rev=601001&r1=601000&r2=601001&view=diff ============================================================================== --- geronimo/sandbox/jetspeed-integration/jetspeed-geronimo/src/main/java/org/apache/geronimo/jetspeed/integration/JetspeedSerializerGBean.java (original) +++ geronimo/sandbox/jetspeed-integration/jetspeed-geronimo/src/main/java/org/apache/geronimo/jetspeed/integration/JetspeedSerializerGBean.java Tue Dec 4 09:03:59 2007 @@ -17,10 +17,13 @@ package org.apache.geronimo.jetspeed.integration; import java.io.File; +import java.io.FileInputStream; import java.io.FilenameFilter; -import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; import java.util.HashMap; import java.util.Map; +import java.util.Properties; import java.net.URL; @@ -30,6 +33,7 @@ import org.apache.jetspeed.components.jndi.SpringJNDIStarter; import org.apache.jetspeed.serializer.JetspeedSerializer; import org.apache.jetspeed.serializer.JetspeedSerializerImpl; +import org.apache.jetspeed.engine.JetspeedEngineConstants; import org.apache.geronimo.kernel.util.ClassLoaderDumper; import org.apache.geronimo.gbean.GBeanInfo; @@ -59,14 +63,18 @@ JetspeedSerializer serializer = null; String applicationPath = getAppRoot(classLoader); - String configFiles = new File(applicationPath, "assembly").getAbsolutePath(); - String bootConfigFiles = new File(configFiles, "boot").getAbsolutePath(); - configFiles = "file:/" + configFiles + File.separator + "*.xml"; - System.out.println(configFiles); - bootConfigFiles = "file:/" + bootConfigFiles + File.separator + "*.xml"; - System.out.println(bootConfigFiles); + File file = new File(applicationPath, "WEB-INF/j2-seed.xml"); + if (!file.exists()) { + System.out.println("Database has already been initialized"); + return; + } + + String fileName = file.getAbsolutePath(); + String[] configFiles = parseFiles(new File(applicationPath, "WEB-INF/assembly").getAbsolutePath()); + String[] bootConfigFiles = new String[] {"file:/" + new File(applicationPath, "WEB-INF/assembly/boot/geronimo-datasource.xml").getAbsolutePath()}; + HashMap context = new HashMap(); context.put(SpringJNDIStarter.DATASOURCE_DRIVER, driverClass); context.put(SpringJNDIStarter.DATASOURCE_URL, url); @@ -87,9 +95,18 @@ * set the application root */ System.out.println("APP ROOT is " + applicationPath); - System.setProperty("applicationRoot",applicationPath); + System.setProperty(JetspeedEngineConstants.APPLICATION_ROOT_KEY, applicationPath); System.setProperty("portal.name","jetspeed"); - SpringJNDIStarter starter = new SpringJNDIStarter(context, applicationPath, new String[]{bootConfigFiles}, new String[]{configFiles}); + Properties p = System.getProperties(); + try { + p.load(new FileInputStream(new File(applicationPath, JetspeedEngineConstants.JETSPEED_PROPERTIES_DEFAULT))); + } + catch (Exception e) { + e.printStackTrace(); + } + System.setProperties(p); + + SpringJNDIStarter starter = new SpringJNDIStarter(context, applicationPath, bootConfigFiles, configFiles); System.out.println("starter framework created " + starter); @@ -107,7 +124,6 @@ Map settings = new HashMap(); settings.put(JetspeedSerializer.KEY_OVERWRITE_EXISTING, Boolean.TRUE); - String fileName = new File(applicationPath + "/j2-seed.xml").getAbsolutePath(); try { System.out.println("processing import " + fileName ); serializer = new JetspeedSerializerImpl(starter.getComponentManager()); @@ -132,24 +148,29 @@ e1.printStackTrace(); } + + cleanup(fileName, bootConfigFiles); } + + /** + * Retrieves the app root directory of the geronimo-tomcat configuration + */ private String getAppRoot(ClassLoader classLoader) { + // I think this is a hack. There must be a more elegant way to get the app root + // From the classloader, we get the jar inside which this class is bundled. + // We then strip out the trailing end of the classpath to get the root. URL url = classLoader.getResource("org/apache/jetspeed/components/jndi/SpringJNDIStarter.class"); if (url == null) { System.out.println("SpringJNDIStarter.class is not in classpath"); return null; } - System.out.println("getFile " + url.getFile()); String path = url.getFile(); int index = path.lastIndexOf("!"); path = path.substring(0, index); - System.out.println(path); path = path.substring(path.indexOf("/") + 1); - System.out.println(path); - path = new File(path).getParentFile().getParent(); - System.out.println(path); + path = new File(path).getParentFile().getParentFile().getParent(); return path; } @@ -173,5 +194,94 @@ public static GBeanInfo getGBeanInfo() { return GBEAN_INFO; + } + + + /** + * Filters the xml files in WEB-INF/assembly dir and return only those that are needed to populate the database. + * + * @return one or more files to be processed + */ + static private String[] parseFiles(String schemaDirectory) { + final List list = Arrays.asList("cache.xml", "capabilities.xml", "db-page-manager.xml", "interceptors.xml", "prefs.xml", "profiler.xml", "registry.xml", "request-context.xml", "transaction.xml"); + + String[] fileList = null; + try { + File dir = new File(schemaDirectory); + if ( !(dir.exists()) ) { + return fileList; + } + if ( !(dir.isDirectory()) ) { + fileList = new String[1]; + fileList[0] = schemaDirectory; + return fileList; + } + // Handling a directory + File[] files = dir.listFiles( + new FilenameFilter() { + public boolean accept(File dir, String name) + {String n = name.toLowerCase(); + if (list.contains(n)) { + return true; + } + if (n.startsWith("security-")) { + return true; + } + return false; + } + }); + if ( files == null ) + return fileList; + + fileList = new String[files.length]; + for ( int i = 0; i < files.length; i++ ) { + fileList[i] = "file:/" + files[i].getAbsolutePath(); + } + return fileList; + } + catch ( Exception e ) { + e.printStackTrace(); + throw new IllegalArgumentException( + "Processing the schema-directory " + schemaDirectory + + " caused exception " + + e.getLocalizedMessage()); + } + + + } + + /** + * Renamed the j2-ssed.xml and geronimo-datasource.xml to *.used format. + * When this gbean is run again, it will check for the existence of these xmls. + * If they don't exist, the database will not be populated again. + * Some other files that are needed only for db population are deleted. + */ + public void cleanup(String fileName, String[] bootFiles) { + String gdXml = bootFiles[0]; + int index = gdXml.indexOf("/"); + File file = new File(gdXml.substring(index+1)); + if (file.exists()) { + file.renameTo(new File(file.getParentFile(), "geronimo-datasource.used")); + } + + file = new File(fileName); + File parentFile = file.getParentFile(); + if (file.exists()) { + file.renameTo(new File(parentFile, "j2-seed.used")); + } + + + file = new File(parentFile, "assembly/db-page-manager.xml"); + if (file.exists()) { + file.delete(); + } + file = new File(parentFile, "assembly/resource-context.xml"); + if (file.exists()) { + file.delete(); + } + file = new File(parentFile, "assembly/interceptors.xml"); + if (file.exists()) { + file.delete(); + } } }