View Javadoc

1   /* ===================================================================
2    * Copyright 2002-04 SRI International.
3    * Released under the MOZILLA PUBLIC LICENSE Version 1.1
4    * which can be obtained at http://www.mozilla.org/MPL/MPL-1.1.html
5    * This software is distributed on an "AS IS"
6    * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
7    * See the License for the specific language governing rights and
8    * limitations under the License.
9    * =================================================================== */
10  package com.sri.emo.wizard.defaults;
11  
12  import com.jcorporate.expresso.core.db.DBException;
13  import com.jcorporate.expresso.core.dbobj.SchemaFactory;
14  import com.sri.common.controller.ComponentServiceLocator;
15  import com.sri.emo.dbobj.WizDefinition;
16  import com.sri.emo.wizard.Wizard;
17  import com.sri.emo.wizard.WizardException;
18  import com.sri.emo.wizard.WizardFactory;
19  import com.sri.emo.wizard.WizardFactorySPI;
20  
21  import java.io.IOException;
22  import java.io.ObjectInputStream;
23  import java.io.ObjectOutputStream;
24  import java.io.Serializable;
25  
26  
27  /***
28   * Version that allows, not only dynamic runtime, but also checks the
29   * underlying Expresso data objects for defined wizards.  It provides
30   *
31   * @author Michael Rimov
32   */
33  
34  public class ExpressoAwareWizardRepository extends DefaultWizardRepository implements Serializable {
35  
36      /***
37  	 * 
38  	 */
39  	private static final long serialVersionUID = 1L;
40  
41  	private transient ComponentServiceLocator serviceLocator;
42  
43      /***
44       * Total hack alert: Because of serializability needs, I can't serialize
45       * the whole service locator -- For now since servicelocator is a Schema
46       * I query schema factory upon rebuild.
47       *
48       * @todo Clean me up
49       */
50      private String schemaClass;
51  
52      public ExpressoAwareWizardRepository(ComponentServiceLocator componentLocator) {
53          serviceLocator = componentLocator;
54          schemaClass = componentLocator.getClass().getName();
55      }
56  
57      /***
58       * Provides bridging of not only the usual find, but also in comparison
59       * to security requests.
60       *
61       * @param id the wizard id.  This implementation expects an integer.
62       * @return Wizard instance.
63       * @throws com.sri.emo.wizard.WizardException
64       *
65       */
66      public synchronized Wizard find(final Serializable id)
67              throws WizardException {
68  
69          Wizard result = null;
70  
71          try {
72              result = super.find(id);
73          } catch (WizardException ex) {
74  //            // do not complain since we have an alternative
75              
76  //            if (LOG.isDebugEnabled()) {
77  //                LOG.debug("Wizard not found for id: " + id, ex);
78  //            }
79          }
80  
81          if (result == null) {
82              result = loadWizard(((Integer) id).toString());
83          }
84  
85          return result;
86      }
87  
88  
89      /***
90       * Loads the wizard.
91       *
92       * @param id the wizard id.
93       * @return Wizard instance.
94       * @throws com.sri.emo.wizard.WizardException
95       *          upon factory construction error, database error,
96       *          or wizard construction error.
97       */
98      protected Wizard loadWizard(final String id) throws WizardException {
99          try {
100             WizDefinition wizdef = new WizDefinition();
101             wizdef.setField(WizDefinition.FLD_ID, id);
102             wizdef.retrieve();
103 
104             String factory = wizdef.getField(WizDefinition.FLD_FACTORY);
105             WizardFactory factoryInstance = (WizardFactory) serviceLocator.locate(factory);
106             if (factoryInstance instanceof WizardFactorySPI) {
107                 ((WizardFactorySPI) factoryInstance).setWizardId(Integer.parseInt(id));
108             }
109             return factoryInstance.buildWizard();
110 
111         } catch (DBException ex) {
112             throw new WizardException("Error accessing database while " +
113                     "building wizard", ex);
114         }
115     }
116 
117     /***
118      * TOTAL HACK Part 2:  Se description of Schema class for more information about this.
119      *
120      * @param ois ObjectInputStream
121      * @throws IOException
122      */
123     private void readObject(ObjectInputStream ois) throws IOException {
124         schemaClass = ois.readUTF();
125         serviceLocator = (ComponentServiceLocator) SchemaFactory.getInstance().getSchema(schemaClass);
126     }
127 
128     private void writeObject(ObjectOutputStream oos) throws IOException {
129         oos.writeUTF(schemaClass);
130     }
131 
132 }