01: /*
02: * Copyright 2007 Ahmed Hashim
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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;
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
34: Creator {
35: /**
36: * What sort of class do we create?
37: * @param classname The name of the class
38: */
39: public void setClass(String classname) {
40: try {
41: clazz = LocalUtil.classForName(classname);
42: } catch (ClassNotFoundException ex) {
43: throw new IllegalArgumentException(Messages.getString(
44: "Creator.ClassNotFound", classname));
45: }
46: }
47:
48: /* (non-Javadoc)
49: * @see org.directwebremoting.extend.Creator#getInstance()
50: */
51: public Object getInstance() throws InstantiationException {
52: try {
53: Method method = clazz.getMethod(factoryMethod);
54: return method.invoke(new SingletonCreator());
55: } catch (Exception ex) {
56: // JDK5: We should really be passing the exception on
57: throw new InstantiationException(Messages
58: .getString("Creator.IllegalAccess"));
59: }
60: }
61:
62: /* (non-Javadoc)
63: * @see org.directwebremoting.extend.Creator#getType()
64: */
65: public Class<?> getType() {
66: return clazz;
67: }
68:
69: /**
70: * @return the factoryMethod function name
71: * */
72: public String getFactoryMethod() {
73: return factoryMethod;
74: }
75:
76: /**
77: * @param functionToCall the name of the factory function.
78: * */
79: public void setFactoryMethod(String functionToCall) {
80: this .factoryMethod = functionToCall;
81: }
82:
83: /**
84: * The function which will return an instance from the object, the common
85: * function name used in singleton class is 'getInstance', this will be the
86: * default value.
87: * */
88: private String factoryMethod = "getInstance";
89:
90: /**
91: * The type of the object that we create
92: */
93: private Class<?> clazz;
94: }
|