KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > base > container > BeanShellContainer


1 /*
2  * $Id: BeanShellContainer.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2004 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.base.container;
26
27 import bsh.Interpreter;
28 import bsh.EvalError;
29
30 import org.ofbiz.base.util.Debug;
31
32 /**
33  * BeanShellContainer - Container implementation for BeanShell
34  *
35  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
36   *@version $Rev: 5462 $
37  * @since 3.0
38  */

39 public class BeanShellContainer implements Container {
40
41     public static final String JavaDoc module = BeanShellContainer.class.getName();
42
43     protected String JavaDoc configFileLocation = null;
44     protected Interpreter bsh = null;
45     protected String JavaDoc name;
46     protected int port;
47
48     /**
49      * @see org.ofbiz.base.container.Container#init(java.lang.String[], java.lang.String)
50      */

51     public void init(String JavaDoc[] args, String JavaDoc configFile) {
52         this.configFileLocation = configFile;
53     }
54     
55     /**
56      * @see org.ofbiz.base.container.Container#init(java.lang.String[], java.lang.String)
57      */

58     public boolean start() throws ContainerException {
59         // get the container config
60
ContainerConfig.Container cfg = ContainerConfig.getContainer("beanshell-container", configFileLocation);
61
62         // get the app-name
63
ContainerConfig.Container.Property appName = cfg.getProperty("app-name");
64         if (appName == null || appName.value == null || appName.value.length() == 0) {
65             throw new ContainerException("Invalid app-name defined in container configuration");
66         } else {
67             this.name = appName.value;
68         }
69
70         // get the telnet-port
71
ContainerConfig.Container.Property telnetPort = cfg.getProperty("telnet-port");
72         if (telnetPort == null || telnetPort.value == null || telnetPort.value.length() == 0) {
73             throw new ContainerException("Invalid telnet-port defined in container configuration");
74         } else {
75             try {
76                 this.port = Integer.parseInt(telnetPort.value);
77             } catch (Exception JavaDoc e) {
78                 throw new ContainerException("Invalid telnet-port defined in container configuration; not a valid int");
79             }
80         }
81
82         // create the interpreter
83
bsh = new Interpreter();
84
85         // configure the interpreter
86
if (bsh != null) {
87             try {
88                 bsh.set(name, this);
89             } catch (EvalError evalError) {
90                 throw new ContainerException(evalError);
91             }
92             try {
93                 bsh.set("portnum", (port - 1));
94             } catch (EvalError evalError) {
95                 throw new ContainerException(evalError);
96             }
97             try {
98                 bsh.eval("setAccessibility(true)");
99             } catch (EvalError evalError) {
100                 throw new ContainerException(evalError);
101             }
102
103             try {
104                 bsh.eval("server(portnum)");
105             } catch (EvalError evalError) {
106                 throw new ContainerException(evalError);
107             }
108
109             Debug.logInfo("Started BeanShell telnet service on " + (port - 1) + ", " + port, module);
110             Debug.logInfo("NOTICE: BeanShell service ports are not secure. Please protect the ports", module);
111             return true;
112         } else {
113             return false;
114         }
115     }
116
117     /**
118      * Stop the container
119      *
120      * @throws ContainerException
121      *
122      */

123     public void stop() throws ContainerException {
124         bsh = null;
125     }
126 }
127
Popular Tags