View Javadoc
1 /*** $Id: DAOFactory.java,v 1.1.1.1 2004/05/18 10:50:12 mochoa Exp $ */ 2 package org.j2ee.dao; 3 4 //java 5 import java.util.ResourceBundle; 6 import java.util.MissingResourceException; 7 import java.util.Locale; 8 import java.util.Map; 9 import java.util.HashMap; 10 import java.util.Vector; 11 import java.util.StringTokenizer; 12 //io 13 import java.io.FileInputStream; 14 import java.io.File; 15 import java.io.IOException; 16 // JNDI 17 import javax.naming.InitialContext; 18 import javax.naming.Context; 19 import javax.naming.NamingException; 20 21 /*** 22 * Initial Factory for the DAO Pattern 23 * It will locate daofactory.properties file in four differente way: 24 * First: 25 * Test for the system propertie: org.j2ee.dao.factory 26 * and try to open it as File 27 * Usage: java -Dorg.j2ee.dao.factory=file:/tmp/daofactory.properties 28 * 29 * Second: 30 * Test for the JNDI name: java:comp/env/daoFactoryProperty 31 * and try to open it as ResourceBoundle class loader spec 32 * Usage (Define it in your ejb-jar.xml): 33 * <env-entry> 34 * <env-entry-name>daoFactoryProperty</env-entry-name> 35 * <env-entry-type>java.lang.String</env-entry-type> 36 * <env-entry-value>my.pkg.daofactory</env-entry-value> 37 * </env-entry> 38 * 39 * Third: 40 * Test in the root classpath 41 * and try to load it using Resource Boundle semantic 42 * Usage: put daofactory.properties in the root classpath of your .jar 43 * 44 * Fourth: 45 * Test in the DAO classpath (org.j2ee.dao) 46 * and try to load it using Resource Boundle semantic 47 * Usage: put daofactory.properties in org/j2ee/dao directory of your .jar 48 * 49 * Based on an example of book Code Notes for J2EE, Edited by GREGORY BRILL, 50 * e-ISBN 0-679-64727-9 51 * 52 * Last modified $Date: 2004/05/18 10:50:12 $ 53 * @author jvlio (jvlio@users.sourceforge.net) 54 * @version $Revision: 1.1.1.1 $ 55 * 56 * @see ResourceBundle 57 */ 58 public abstract class DAOFactory { 59 60 public static final String FACTORIES_ENTRY="factories"; 61 public static final String DEFAULT_FACTORY_ENTRY = FACTORIES_ENTRY+"."+"default"; 62 public static final String FACTORY_CLASS_SUFFIX="class"; 63 64 public static final String ACTIONS_ENTRY="actions"; 65 public static final String GROUPS_ENTRY="groups"; 66 public static final String ACTION_CLASS_SUFFIX="class"; 67 68 protected static Map factoryMap = null; 69 private static String defaultFactory = null; 70 protected static ResourceBundle rb = null; 71 72 static { 73 factoryMap = new HashMap(); 74 //@todo leer properties con los factories 75 String fname = System.getProperty("org.j2ee.dao.factory"); 76 if (fname!=null) { 77 try { 78 rb = new java.util.PropertyResourceBundle(new FileInputStream(new File(fname))); 79 } catch (IOException ioex) { 80 System.out.println(" ["+System.currentTimeMillis()+"] DAOFactory.static: Factory properties "+fname+" not found"); 81 } 82 } else { // Try to find JDNI properties 83 String daoFactoryFileName = null; 84 try { 85 Context ctx = new InitialContext(); 86 daoFactoryFileName = (String)ctx.lookup("java:comp/env/daoFactoryProperty"); 87 } 88 catch (NamingException e) { 89 daoFactoryFileName = "daofactory"; 90 } 91 try { // search in root classpath 92 rb = ResourceBundle.getBundle(daoFactoryFileName,Locale.getDefault()); 93 } catch (MissingResourceException mrex1) { 94 try { // search in dao package 95 rb = ResourceBundle.getBundle("org.j2ee.dao.daofactory",Locale.getDefault()); 96 } catch (MissingResourceException mrex2) {} 97 } 98 } 99 //find factory names 100 String sfactories = getResourceString(rb,FACTORIES_ENTRY); 101 defaultFactory = getResourceString(rb,DEFAULT_FACTORY_ENTRY); 102 if (sfactories!=null) { 103 String factories[] = tokenize(sfactories); 104 String sclass = null; 105 for (int i=0; i<factories.length; i++) { 106 sclass = getResourceString(rb,factories[i]+"."+FACTORY_CLASS_SUFFIX); 107 factoryMap.put(factories[i],sclass); 108 } 109 } 110 //if not configured factories, then create default entry. 111 if (factoryMap.isEmpty()) { 112 defaultFactory = "default"; 113 factoryMap.put(defaultFactory,"org.j2ee.dao.DefaultDAOFactoryImpl"); 114 } 115 } 116 117 protected abstract void inizialize(String factoryName); 118 119 120 /*** Returns a default factory 121 */ 122 public final static DAOFactory getDAOFactory() { return getDAOFactory(defaultFactory); } 123 124 /*** Search in factories Map if find a key named "whichFactory", if find 125 * is succefully then compare the object instance, if is a string, then 126 * load class named in this way, otherwise the object is a Factory. 127 * If the key not found, then i guest is a class name and load this 128 * Factory class. 129 */ 130 public final static DAOFactory getDAOFactory(String whichFactory) { 131 Object oFactory = factoryMap.get(whichFactory); 132 if (oFactory==null) return null; // todo OJO!! throw Exception!!! 133 if (oFactory instanceof String) { 134 // if the factory not is instanciated, then intanciate this and inizialice. 135 String factoryName = (String)oFactory; 136 DAOFactory aFactory = null; 137 try { 138 aFactory = (DAOFactory)Class.forName(factoryName).newInstance(); 139 } catch (Exception ex) { return null; } 140 // inizialize factory 141 aFactory.inizialize(factoryName); 142 // put factory instance in map 143 factoryMap.put(whichFactory,aFactory); 144 return aFactory; 145 } 146 else 147 return (DAOFactory)oFactory; 148 } 149 150 public static ResourceBundle getProperties() { 151 return rb; 152 } 153 154 protected static String getResourceString(ResourceBundle resources, String key) { 155 String str; 156 try { 157 str = resources.getString(key); 158 } catch (MissingResourceException mre) { 159 str = null; 160 } 161 return str; 162 } 163 164 165 /*** 166 * Take the given string and chop it up into a series 167 * of strings on whitespace boundries. This is useful 168 * for trying to get an array of strings out of the 169 * resource file. 170 */ 171 protected static String[] tokenize(String input) { 172 Vector v = new Vector(); 173 StringTokenizer t = new StringTokenizer(input); 174 String cmd[]; 175 176 while (t.hasMoreTokens()) 177 v.addElement(t.nextToken()); 178 cmd = new String[v.size()]; 179 for (int i = 0; i < cmd.length; i++) 180 cmd[i] = (String) v.elementAt(i); 181 182 return cmd; 183 } 184 185 186 187 public abstract DAOActionProcessor getProcessor(); 188 public abstract DAOActionProcessor getProcessor(String processor_name); 189 public abstract DAOAction getAction(String action_name); 190 }

This page was automatically generated by Maven