KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2005 Joe Walker
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 org.directwebremoting.extend.Creator;
19 import org.directwebremoting.util.LocalUtil;
20 import org.directwebremoting.util.Logger;
21 import org.directwebremoting.util.Messages;
22
23 /**
24  * A creator that simply uses the default constructor each time it is called.
25  * @author Joe Walker [joe at getahead dot ltd dot uk]
26  */

27 public class NewCreator extends AbstractCreator implements Creator
28 {
29     /**
30      * What sort of class do we create?
31      * @param classname The name of the class
32      */

33     public void setClass(String JavaDoc classname)
34     {
35         try
36         {
37             clazz = LocalUtil.classForName(classname);
38         }
39         catch (ExceptionInInitializerError JavaDoc ex)
40         {
41             log.warn("Class load error", ex);
42             throw new IllegalArgumentException JavaDoc(Messages.getString("Creator.ClassLoadError", classname));
43         }
44         catch (ClassNotFoundException JavaDoc ex)
45         {
46             throw new IllegalArgumentException JavaDoc(Messages.getString("Creator.ClassNotFound", classname));
47         }
48     }
49
50     /* (non-Javadoc)
51      * @see org.directwebremoting.Creator#getType()
52      */

53     public Class JavaDoc getType()
54     {
55         return clazz;
56     }
57
58     /* (non-Javadoc)
59      * @see org.directwebremoting.Creator#getInstance()
60      */

61     public Object JavaDoc getInstance() throws InstantiationException JavaDoc
62     {
63         try
64         {
65             return clazz.newInstance();
66         }
67         catch (IllegalAccessException JavaDoc ex)
68         {
69             // JDK5: We should really be passing the exception on
70
throw new InstantiationException JavaDoc(Messages.getString("Creator.IllegalAccess"));
71         }
72     }
73
74     /**
75      * Sets the class name to create.
76      * @param className The name of the class to create
77      */

78     public void setClassName(String JavaDoc className)
79     {
80         setClass(className);
81     }
82
83     /**
84      * Gets the name of the class to create.
85      * @return The name of the class to create
86      */

87     public String JavaDoc getClassName()
88     {
89         return getType().getName();
90     }
91
92     /**
93      * The log stream
94      */

95     private static final Logger log = Logger.getLogger(NewCreator.class);
96
97     /**
98      * The type of the class that we are creating
99      */

100     private Class JavaDoc clazz;
101 }
102
Popular Tags