KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > securitymgr > ejb > IOStatelessSessionBean


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software 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 software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.securitymgr.ejb;
23
24 import java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.lang.SecurityManager JavaDoc;
27 import java.lang.reflect.Field JavaDoc;
28 import java.net.ServerSocket JavaDoc;
29 import java.net.Socket JavaDoc;
30 import java.security.Permission JavaDoc;
31 import java.security.Principal JavaDoc;
32 import javax.ejb.CreateException JavaDoc;
33 import javax.ejb.EJBException JavaDoc;
34 import javax.ejb.SessionBean JavaDoc;
35 import javax.ejb.SessionContext JavaDoc;
36
37 import org.jboss.logging.Logger;
38
39 import org.jboss.security.SecurityAssociation;
40
41 /** A session bean that attempts operations not allowed by the EJB 2.0
42  spec as a test of running JBoss with a security manager.
43  
44 @author Scott.Stark@jboss.org
45 @version $Revision: 58115 $
46  */

47 public class IOStatelessSessionBean implements SessionBean JavaDoc
48 {
49    static final Logger log = Logger.getLogger(IOStatelessSessionBean.class);
50
51    private SessionContext JavaDoc sessionContext;
52
53    public void ejbCreate() throws CreateException JavaDoc
54    {
55    }
56    public void ejbActivate()
57    {
58    }
59    public void ejbPassivate()
60    {
61    }
62    public void ejbRemove()
63    {
64    }
65
66    public void setSessionContext(SessionContext JavaDoc context)
67    {
68       sessionContext = context;
69    }
70
71    /**
72     */

73    public String JavaDoc read(String JavaDoc path) throws IOException JavaDoc
74    {
75       log.debug("read, path="+path);
76       File JavaDoc tstPath = new File JavaDoc(path);
77       if( tstPath.exists() == false )
78          path = null;
79       return path;
80    }
81
82    public void write(String JavaDoc path) throws IOException JavaDoc
83    {
84       log.debug("write, path="+path);
85       File JavaDoc tstPath = new File JavaDoc(path);
86       tstPath.createNewFile();
87    }
88
89    public void listen(int port) throws IOException JavaDoc
90    {
91       log.debug("Creating server listening port: "+port);
92       ServerSocket JavaDoc ss = new ServerSocket JavaDoc(port);
93       log.debug("Listening");
94       ss.close();
95    }
96
97    public void connect(String JavaDoc host, int port) throws IOException JavaDoc
98    {
99       log.debug("connect, host: "+host+", port: "+port);
100       Socket JavaDoc s = new Socket JavaDoc(host, port);
101       log.debug("Connected");
102       s.close();
103    }
104
105    public void createClassLoader()
106    {
107       log.debug("createClassLoader");
108       // Can't use URLClassLoader.newInstance as this uses a privaledged block
109
ClassLoader JavaDoc cl = new ClassLoader JavaDoc()
110          {
111          };
112       log.debug("Created ClassLoader");
113    }
114    public void getContextClassLoader()
115    {
116       // This will be allowed because the our class loader is an ancestor of the TCL
117
log.debug("Begin getContextClassLoader");
118       ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
119       log.debug("End getContextClassLoader");
120    }
121    public void setContextClassLoader()
122    {
123       log.debug("Begin setContextClassLoader");
124       ClassLoader JavaDoc cl = null;
125       Thread.currentThread().setContextClassLoader(cl);
126       log.debug("End setContextClassLoader");
127    }
128    public void createSecurityMgr()
129    {
130       log.debug("createSecurityMgr");
131       SecurityManager JavaDoc secmgr = new SecurityManager JavaDoc()
132       {
133          public void checkPermission(Permission JavaDoc p)
134          {
135          }
136       };
137       System.setSecurityManager(secmgr);
138    }
139
140    /** This will only be disallowed if the current thread belongs to the
141     root thread group and this is rarely true as even the thread that
142     starts main() is not in this group.
143     */

144    public void renameThread()
145    {
146       log.debug("renameThread");
147       Thread JavaDoc t = Thread.currentThread();
148       t.setName("Hijacked name");
149       log.debug("Renamed current thread");
150    }
151    public void createThread()
152    {
153       log.debug("createThread");
154       Thread JavaDoc t = new Thread JavaDoc("IOSession.createThread");
155       t.start();
156       log.debug("Started a thread");
157    }
158
159    /** This test will only fail if reflection is used on a class that
160     has not been loaded by the same class loader as the IOStatelessSessionBean
161     */

162    public void useReflection()
163    {
164       log.debug("useReflection");
165       try
166       {
167          Field JavaDoc secret = System JavaDoc.class.getDeclaredField("secret");
168          Object JavaDoc value = secret.get(null);
169       }
170       catch(NoSuchFieldException JavaDoc e)
171       {
172       }
173       catch(IllegalAccessException JavaDoc e)
174       {
175       }
176       log.debug("Search for System.secret did not fail with a SecurityException");
177    }
178
179    public void loadLibrary()
180    {
181       log.debug("loadLibrary");
182       System.loadLibrary("jdwp");
183       log.debug("Called System.loadLibrary");
184    }
185
186    public void changeSystemOut()
187    {
188       log.debug("changeSystemOut");
189       System.setOut(null);
190    }
191    public void changeSystemErr()
192    {
193       log.debug("changeSystemErr");
194       System.setErr(null);
195    }
196
197    public void systemExit(int status)
198    {
199       log.debug("systemExit");
200       System.exit(status);
201    }
202
203 }
204
Popular Tags