KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > explorer > core > code > lib > BeanShellCode


1 /*====================================================================
2
3 Objectweb Explorer Framework
4 Copyright (C) 2005 INRIA - USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Philippe Merle.
23 Contributor(s): ______________________________________.
24
25 ====================================================================
26 $Id: BeanShellCode.java,v 1.1 2005/05/30 10:47:22 merle Exp $
27 ====================================================================*/

28
29 package org.objectweb.util.explorer.core.code.lib;
30
31 import org.objectweb.util.explorer.ExplorerUtils;
32 import org.objectweb.util.explorer.core.code.api.Code;
33 import org.objectweb.util.explorer.core.code.api.CodeConfiguration;
34 import org.objectweb.util.explorer.core.common.lib.ClassResolver;
35 import org.objectweb.util.trace.TraceSystem;
36
37 import bsh.Interpreter;
38 import bsh.EvalError;
39
40 /**
41  *
42  *
43  * @author <a HREF="mailto:Philippe.Merle@inria.fr">Philippe Merle</a>.
44  *
45  * @version 0.1
46  */

47 public class BeanShellCode
48   implements CodeConfiguration, Code
49 {
50
51     //==================================================================
52
//
53
// Internal state.
54
//
55
// ==================================================================
56

57     /** The code to instanciate. */
58     protected String JavaDoc theCode_ = null;
59
60     protected Class JavaDoc theClass_ = null;
61    
62     // ==================================================================
63
//
64
// Constructors.
65
//
66
// ==================================================================
67

68     // ==================================================================
69
//
70
// No internal method.
71
//
72
// ==================================================================
73

74     protected boolean equals(BeanShellCode code){
75         if(code!=null){
76             String JavaDoc theCode = code.theCode_;
77             if(theCode_!=null && theCode!=null){
78                 return theCode_.equals(theCode);
79             }
80         }
81         return false;
82     }
83     
84     // ==================================================================
85
//
86
// Public methods for CodeConfiguration interface.
87
//
88
// ==================================================================
89

90     public void setCode(String JavaDoc code) {
91         theCode_ = code;
92     }
93
94     // ==================================================================
95
//
96
// Public methods for Code interface.
97
//
98
// ==================================================================
99

100     /* (non-Javadoc)
101      * @see org.objectweb.util.explorer.core.code.api.Code#isAssignableFrom(java.lang.Class)
102      */

103     public boolean isInstanceOf(Class JavaDoc c) {
104         theClass_ = c;
105         return true;
106     }
107     
108     /* (non-Javadoc)
109      * @see org.objectweb.util.explorer.core.code.api.Code#createInstance()
110      */

111     public Object JavaDoc createInstance() {
112         Interpreter interpreter = new Interpreter();
113
114         String JavaDoc code = theCode_
115                     + "\nreturn ("
116                     + theClass_.getName()
117                     + ")this;";
118         try
119         {
120             return interpreter.eval(code);
121         }
122         catch(EvalError exc)
123         {
124             TraceSystem.get("explorer").info(exc.getMessage());
125         }
126     return null;
127     }
128
129     // ==================================================================
130
//
131
// Public methods surcharging Object class.
132
//
133
// ==================================================================
134

135     /**
136      * Surcharging the Object.equals method.
137      * @see Object#equals(java.lang.Object)
138      */

139     public boolean equals(Object JavaDoc o){
140         if(o!=null && o instanceof BeanShellCode){
141             return equals((BeanShellCode)o);
142         }
143         return false;
144     }
145    
146     /**
147      * Return a String representation of a BasicImageIcon
148      * @see Object#toString()
149      */

150     public String JavaDoc toString(){
151         return "BeanShellCode[class=" +
152             ExplorerUtils.toString(theClass_) + "]";
153     }
154         
155 }
156
Popular Tags