Java tutorial
package com.web.server; /*Copyright 2013 - 2015, Arun_Soundararajan (arun_srajan_2007@yahoo.com).and/or its affiliates. All files in this repository or distribution are licensed under the Apache License, Version 2.0 (the "License"); you may not use any files in this repository or distribution except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.*/ import java.beans.PropertyVetoException; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; import java.util.HashMap; import java.util.concurrent.CopyOnWriteArrayList; import javax.naming.InitialContext; import javax.naming.NamingException; import org.apache.commons.digester3.Digester; import org.apache.commons.digester3.binder.DigesterLoader; import org.apache.commons.digester3.xmlrules.FromXmlRulesModule; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import com.mchange.v2.c3p0.ComboPooledDataSource; import com.web.server.util.DirectoryWatcher; public class XMLDeploymentScanner extends DirectoryWatcher implements XMLDeploymentMBean { Digester xmlDigester; InitialContext ic; HashMap<String, XMLDataSources> xmlmap = new HashMap<String, XMLDataSources>(); WebClassLoader userLibJarLoader; String userLibDir; public XMLDeploymentScanner(String scanDirectory, String userLibDir) { super(scanDirectory); this.userLibDir = userLibDir; File userLibJars = new File(userLibDir); CopyOnWriteArrayList<String> jarList = new CopyOnWriteArrayList(); getUsersJars(userLibJars, jarList); URLClassLoader loader = (URLClassLoader) ClassLoader.getSystemClassLoader(); userLibJarLoader = new WebClassLoader(loader.getURLs()); for (String jarFilePath : jarList) { try { userLibJarLoader.addURL(new URL("file:/" + jarFilePath.replace("\\", "/"))); } catch (MalformedURLException e2) { // TODO Auto-generated catch block e2.printStackTrace(); } } DigesterLoader xmlDigesterLoader = DigesterLoader.newLoader(new FromXmlRulesModule() { protected void loadRules() { // TODO Auto-generated method stub try { loadXMLRules(new InputSource(new FileInputStream("./config/datasource-rules.xml"))); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); xmlDigester = xmlDigesterLoader.newDigester(); try { ic = new InitialContext(); ic.createSubcontext("java:"); } catch (NamingException e1) { // TODO Auto-generated catch block } File[] xmlFiles = new File(scanDirectory).listFiles(); if (xmlFiles != null && xmlFiles.length > 0) { for (File xmlFile : xmlFiles) { if (xmlFile.getName().toLowerCase().endsWith("datasource.xml")) { installDataSource(xmlFile); } } } // TODO Auto-generated constructor stub } public void getUsersJars(File dir, CopyOnWriteArrayList jarList) { if (dir.isDirectory()) { File[] children = dir.listFiles(); for (int i = 0; i < children.length; i++) { System.out.println(children[i]); getUsersJars(children[i], jarList); if (children[i].isFile() && children[i].getName().endsWith(".jar")) jarList.add(children[i].getAbsolutePath()); } } } @Override protected void onDelete(File file) { System.out.println(file.getAbsolutePath()); try { XMLDataSources xmlDataSources = xmlmap.get(xmlmap); for (XMLDataSource xmlDataSource : xmlDataSources.xmlDataSources) { if (xmlDataSource != null) ic.unbind("java:/" + xmlDataSource.getJndi()); } } catch (NamingException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(file.getAbsolutePath()); } public void installDataSource(File file) { ClassLoader ctxCLassLoader = null; try { XMLDataSources xmlDataSources = (XMLDataSources) xmlDigester.parse("file:///" + file.getAbsolutePath()); ctxCLassLoader = Thread.currentThread().getContextClassLoader(); Thread.currentThread().setContextClassLoader(userLibJarLoader); CopyOnWriteArrayList<XMLDataSource> xmlDataSourceList = xmlDataSources.xmlDataSources; xmlmap.put(file.getAbsolutePath(), xmlDataSources); for (XMLDataSource xmlDataSource : xmlDataSourceList) { ComboPooledDataSource cpds = new ComboPooledDataSource(); cpds.setDriverClass(xmlDataSource.getDriverclass()); cpds.setJdbcUrl(xmlDataSource.getJdbcurl()); cpds.setUser(xmlDataSource.getUsername()); cpds.setPassword(xmlDataSource.getPassword()); if (xmlDataSource.getMinpoolsize() != null) { cpds.setMinPoolSize(new Integer(xmlDataSource.getMinpoolsize())); } if (xmlDataSource.getAcquireincrement() != null) { cpds.setAcquireIncrement(new Integer(xmlDataSource.getAcquireincrement())); } if (xmlDataSource.getAcquireincrement() != null) { cpds.setMaxPoolSize(new Integer(xmlDataSource.getMaxpoolsize())); } if (xmlDataSource.getMaxstatements() != null) { cpds.setMaxStatements(new Integer(xmlDataSource.getMaxstatements())); } if (xmlmap.get(file.getAbsolutePath()) == null) { ic.bind("java:/" + xmlDataSource.getJndi(), cpds); } else { ic.rebind("java:/" + xmlDataSource.getJndi(), cpds); } } } catch (Exception ex) { ex.printStackTrace(); } finally { if (ctxCLassLoader != null) { Thread.currentThread().setContextClassLoader(ctxCLassLoader); } } } @Override protected void onAdd(File file) { System.out.println(file.getAbsolutePath()); if (file.getName().toLowerCase().endsWith("datasource.xml")) { installDataSource(file); } System.out.println(file.getAbsolutePath()); } @Override protected void onModify(File file) { System.out.println(file.getAbsolutePath()); if (file.getName().toLowerCase().endsWith("datasource.xml")) { installDataSource(file); } System.out.println(file.getAbsolutePath()); } }