KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > create > SingletonCreator


1 /*
2  * Copyright 2007 Ahmed Hashim
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.directwebremoting.create;
17
18 import java.lang.reflect.Method JavaDoc;
19
20 import org.directwebremoting.extend.Creator;
21 import org.directwebremoting.util.LocalUtil;
22 import org.directwebremoting.util.Messages;
23
24 /**
25  * A {@link Creator} that uses an instance method to create singletons.
26  * <p>By default this creator uses a static method with the signature:
27  * <code>SomeClass.getInstance()</code> to create new instances. The name of
28  * the singleton constructor method can be customized using the
29  * <code>getInstance</code> parameter.
30  * @author Ahmed Hashim [hashim at egjug dot org]
31  * @author Joe Walker [joe at getahead dot ltd dot uk]
32  */

33 public class SingletonCreator extends AbstractCreator implements Creator
34 {
35     /**
36      * What sort of class do we create?
37      * @param classname The name of the class
38      */

39     public void setClass(String JavaDoc classname)
40     {
41         try
42         {
43             clazz = LocalUtil.classForName(classname);
44             System.out.println(clazz.getName());
45         }
46         catch (ClassNotFoundException JavaDoc ex)
47         {
48             throw new IllegalArgumentException JavaDoc(Messages.getString("Creator.ClassNotFound", classname)); //$NON-NLS-1$
49
}
50     }
51
52     /* (non-Javadoc)
53      * @see org.directwebremoting.extend.Creator#getInstance()
54      */

55     public Object JavaDoc getInstance() throws InstantiationException JavaDoc
56     {
57         try
58         {
59             Method JavaDoc method = clazz.getMethod(factoryMethod, null);
60             return method.invoke(new SingletonCreator(), null);
61         }
62         catch (Exception JavaDoc ex)
63         {
64             // JDK5: We should really be passing the exception on
65
throw new InstantiationException JavaDoc(Messages.getString("Creator.IllegalAccess"));
66         }
67     }
68
69     /* (non-Javadoc)
70      * @see org.directwebremoting.extend.Creator#getType()
71      */

72     public Class JavaDoc getType()
73     {
74         return clazz;
75     }
76     
77     /**
78      * @return the factoryMethod function name
79      * */

80     public String JavaDoc getFactoryMethod()
81     {
82         return factoryMethod;
83     }
84     
85     /**
86      * @param functionToCall the name of the factory function.
87      * */

88     public void setFactoryMethod(String JavaDoc functionToCall)
89     {
90         this.factoryMethod = functionToCall;
91     }
92
93     /**
94      * The function which will return an instance from the object, the common
95      * function name used in singleton class is 'getInstance', this will be the
96      * default value.
97      * */

98     private String JavaDoc factoryMethod = "getInstance";
99
100     /**
101      * The type of the object that we create
102      */

103     private Class JavaDoc clazz;
104 }
105
Popular Tags