01: /*
02: * Copyright 2005 Joe Walker
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 org.apache.commons.logging.LogFactory;
19: import org.apache.commons.logging.Log;
20: import org.directwebremoting.extend.Creator;
21: import org.directwebremoting.util.LocalUtil;
22: import org.directwebremoting.util.Messages;
23:
24: /**
25: * A creator that simply uses the default constructor each time it is called.
26: * @author Joe Walker [joe at getahead dot ltd dot uk]
27: */
28: public class NewCreator extends AbstractCreator implements Creator {
29: /**
30: * What sort of class do we create?
31: * @param classname The name of the class
32: */
33: public void setClass(String classname) {
34: try {
35: clazz = LocalUtil.classForName(classname);
36: if (getJavascript() == null) {
37: setJavascript(clazz.getSimpleName());
38: }
39: } catch (ExceptionInInitializerError ex) {
40: log.warn("Class load error", ex);
41: throw new IllegalArgumentException(Messages.getString(
42: "Creator.ClassLoadError", classname));
43: } catch (ClassNotFoundException ex) {
44: throw new IllegalArgumentException(Messages.getString(
45: "Creator.ClassNotFound", classname));
46: }
47: }
48:
49: /* (non-Javadoc)
50: * @see org.directwebremoting.Creator#getType()
51: */
52: public Class<?> getType() {
53: return clazz;
54: }
55:
56: /* (non-Javadoc)
57: * @see org.directwebremoting.Creator#getInstance()
58: */
59: public Object getInstance() throws InstantiationException {
60: try {
61: return clazz.newInstance();
62: } catch (IllegalAccessException ex) {
63: // JDK5: We should really be passing the exception on
64: throw new InstantiationException(Messages
65: .getString("Creator.IllegalAccess"));
66: }
67: }
68:
69: /**
70: * Sets the class name to create.
71: * @param className The name of the class to create
72: */
73: public void setClassName(String className) {
74: setClass(className);
75: }
76:
77: /**
78: * Gets the name of the class to create.
79: * @return The name of the class to create
80: */
81: public String getClassName() {
82: return getType().getName();
83: }
84:
85: /**
86: * The log stream
87: */
88: private static final Log log = LogFactory.getLog(NewCreator.class);
89:
90: /**
91: * The type of the class that we are creating
92: */
93: private Class<?> clazz;
94: }
|