1   //==============================================================================
2   // file :       ViewHelper.java
3   // project:     Front View Control
4   //
5   // last change: date:       $Date: 2003/09/10 09:13:57 $
6   //              by:         $Author: bitiboy $
7   //              revision:   $Revision: 1.1 $
8   //------------------------------------------------------------------------------
9   // copyright:   GNU GPL Software License (see class documentation)
10  //==============================================================================
11  package com.justhis.control;
12  
13  
14  /*
15   * $Id: ViewHelper.java,v 1.1 2003/09/10 09:13:57 bitiboy Exp $
16   *
17   * Copyright 2003 Acai Software All Rights Reserved.
18   *
19   * This file ViewHelper.java is part of the Front View Control.
20  
21   * The Front View Control is free software; you can redistribute it and/or modify
22   * it under the terms of the GNU General Public License as published by
23   * the Free Software Foundation; either version 2 of the License, or
24   * (at your option) any later version.
25  
26   * The Front View Control is distributed in the hope that it will be useful,
27   * but WITHOUT ANY WARRANTY; without even the implied warranty of
28   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29   * GNU General Public License for more details.
30  
31   * You should have received a copy of the GNU General Public License
32   * along with the Front View Control; if not, write to the Free Software
33   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
34  
35   * http://www.justhis.com
36   * CONTACT: email = superaxis@sohu.com webmaster@justhis.com
37   */
38  import com.justhis.util.PropertiesReader;
39  import com.justhis.util.exception.JavaClassRefectException;
40  import com.justhis.util.exception.PropertiesFileReadException;
41  
42  import java.util.Enumeration;
43  import java.util.HashMap;
44  import java.util.Map;
45  
46  
47  /***
48   * A singleton class that creates commands using the command mapping property
49   * file.
50   *
51   * @author <a href="http://blog.ejb.cn">acai</a>
52   * @version $Revision: 1.1 $
53   */
54  public class ViewHelper {
55      //~ Static fields/initializers ---------------------------------------------
56  
57      /*** The name of the property file mapping command names to classes. */
58      private static final java.lang.String COMMAND_MAPPING_PROPERTY_FILE = "/cmdMapping";
59  
60      /*** Mapping of command name to command class. */
61      private static Map commands = null;
62  
63      /*** synchronized sign */
64      private static Object obj = new Object();
65  
66      //~ Constructors -----------------------------------------------------------
67  
68      /***
69       * Construct an instance.
70       */
71      private ViewHelper() {
72      }
73  
74      //~ Methods ----------------------------------------------------------------
75  
76      /***
77       * Create the map of command names to command classes.
78       */
79      private static void createCommandMap()
80                                    throws PropertiesFileReadException, 
81                                           JavaClassRefectException {
82          Map commandMap = new HashMap();
83  
84          PropertiesReader reader = new PropertiesReader(COMMAND_MAPPING_PROPERTY_FILE);
85  
86          for (Enumeration e = reader.getKeys(); e.hasMoreElements();) {
87              String action = (String) e.nextElement();
88              Class newCommandClass = null;
89  
90              try {
91                  newCommandClass = Class.forName(reader.getProperty(action));
92              } catch (ClassNotFoundException e1) {
93                  throw new JavaClassRefectException("Happen ClassNotFoundException",
94                                                     e1
95                                                    );
96              }
97  
98              commandMap.put(action, newCommandClass);
99          }
100 
101         setCommands(commandMap);
102     }
103 
104     /***
105      * Get the map from command names to command classes.
106      *
107      * @return the map from command names to  command classes
108      */
109     private static Map getCommands()
110                             throws PropertiesFileReadException, 
111                                    JavaClassRefectException {
112         synchronized (obj) {
113             if (commands == null) {
114                 createCommandMap();
115             }
116         }
117 
118         return commands;
119     }
120 
121     /***
122      * Set the map from command names to command classes.
123      *
124      * @return commandMap the new map from command names to command classes
125      */
126     private static void setCommands(Map commandMap) {
127         commands = commandMap;
128     }
129 
130     /***
131      * Create a Command instance for the specified action or command.
132      *
133      * @param action the name of the command
134      *
135      * @return Command or <code>null</code> if the command could not be created
136      */
137     public static Command getCommand(String action)
138                               throws PropertiesFileReadException, 
139                                      JavaClassRefectException {
140         Class newCommandClass = (Class) getCommands().get(action);
141 
142         Command newCommand = null;
143 
144         try {
145             newCommand = (Command) newCommandClass.newInstance();
146         } catch (InstantiationException e) {
147             throw new JavaClassRefectException("Happen InstantiationException",
148                                                e
149                                               );
150         } catch (IllegalAccessException e) {
151             throw new JavaClassRefectException("Happen IllegalAccessException",
152                                                e
153                                               );
154         }
155 
156         return newCommand;
157     }
158 }
159 
160 
161 /*
162  * $Log: ViewHelper.java,v $
163  * Revision 1.1  2003/09/10 09:13:57  bitiboy
164  * *** empty log message ***
165  *
166  *
167 */
This page was automatically generated by Maven