1 /*** $Id: DefaultDAOFactoryImpl.java,v 1.1.1.1 2004/05/18 10:50:13 mochoa Exp $ */
2 package org.j2ee.dao;
3
4 import java.util.Map;
5 import java.util.HashMap;
6
7 /***
8 * <br>
9 * Based on an example of book <i>Code Notes for J2EE, Edited by GREGORY BRILL,
10 * e-ISBN 0-679-64727-9</i>
11 * <br>
12 * Last modified $Date: 2004/05/18 10:50:13 $
13 * @version $Revision: 1.1.1.1 $
14 * @author jvlio (jvlio@users.sourceforge.net)*/
15 public class DefaultDAOFactoryImpl extends DAOFactory {
16
17 private String factoryName = null;
18 public static final String defaultProcessorName = "processor";
19
20 /***
21 * @associates Object
22 */
23 private Map actionMap = null;
24
25 private void loadActions(String sactions) {
26 if (sactions!=null) {
27 String[] actions = tokenize(sactions);
28 String sclass = null;
29 for (int i=0; i<actions.length; i++) {
30 //try to find specific class for this factory
31 sclass = getResourceString(rb,actions[i]+"."+factoryName+"."+ACTION_CLASS_SUFFIX);
32 if (sclass==null) //if not specific, then try defacult
33 sclass = getResourceString(rb,actions[i]+"."+ACTION_CLASS_SUFFIX);
34 if (sclass!=null) //if found, put
35 actionMap.put(actions[i],sclass);
36 }
37 }
38 }
39
40 /***
41 * Inicialize this factory, named by <i>factoryName</i> name.
42 * @param factoryName name given to this factory instance.
43 */
44 protected void inizialize(String factoryName) {
45 this.factoryName = factoryName;
46 this.actionMap = new HashMap();
47 //find actions names
48 String sactions = getResourceString(rb,ACTIONS_ENTRY);
49 loadActions(sactions);
50 //find actions groups names
51 String sgroups = getResourceString(rb,GROUPS_ENTRY);
52 if (sgroups!=null) {
53 String[] groups = tokenize(sgroups);
54 for (int i=0; i<groups.length; i++) {
55 sactions = getResourceString(rb,groups[i]+"."+ACTIONS_ENTRY);
56 loadActions(sactions);
57 }
58 }
59 }
60
61 /***
62 * Get or Create a default DAO processor (processor.class entry on daofactory.properties)
63 * @return a DAOActionProcessor
64 */
65 public DAOActionProcessor getProcessor() {
66 return getProcessor(DefaultDAOFactoryImpl.defaultProcessorName);
67 }
68
69 /***
70 * Get or create a named processor
71 * @param processorKey entry for the processor into daofactory.properties.
72 * @return a DAOActionProcessor
73 */
74 public DAOActionProcessor getProcessor(String processorKey) {
75 Object oProcessor = factoryMap.get(processorKey);
76 if (oProcessor==null) return null;
77 if (oProcessor instanceof String)
78 return createNewProcessor((String)oProcessor);
79 else
80 return null;
81 }
82
83 /***
84 * Create a DAO Action Processor
85 * @param processor_name fully class name for the processor
86 * @return DAOActionProcessor instance
87 */
88 private DAOActionProcessor createNewProcessor(String processor_name) {
89 Class cProcessor = null;
90 DAOActionProcessor processor = null;
91 if (processor_name==null) return null;
92 try {
93 cProcessor = Class.forName(processor_name);
94 } catch (ClassNotFoundException e) {
95 e.printStackTrace();
96 return null;
97 }
98 try {
99 processor = (DAOActionProcessor)cProcessor.newInstance();
100 } catch (InstantiationException e) {
101 e.printStackTrace();
102 return null;
103 } catch (IllegalAccessException e) {
104 e.printStackTrace();
105 return null;
106 }
107 return processor;
108 }
109
110 public DAOAction getAction(String action_name) {
111 Object oAction = actionMap.get(action_name);
112 Class cAction = null;
113 if (oAction==null) return null;
114 if (oAction instanceof String) {
115 try {
116 cAction = Class.forName((String)oAction);
117 } catch (ClassNotFoundException e) {
118 e.printStackTrace();
119 return null;
120 }
121 actionMap.put(action_name,cAction);
122 }
123 else
124 cAction = (Class)oAction;
125
126 DAOAction aAction = null;
127 try {
128 aAction = (DAOAction)cAction.newInstance();
129 } catch (InstantiationException e) {
130 e.printStackTrace();
131 return null;
132 } catch (IllegalAccessException e) {
133 e.printStackTrace();
134 return null;
135 }
136
137 return aAction;
138 }
139
140 }
This page was automatically generated by Maven