KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > foedeployer > ejb > m2mb > M2MBManagerBean


1 /*
2 * JBoss, the OpenSource EJB server
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */

7 package org.jboss.test.foedeployer.ejb.m2mb;
8
9 import java.sql.Date JavaDoc;
10 import java.util.Collection JavaDoc;
11 import java.util.HashSet JavaDoc;
12 import java.util.Set JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.ArrayList JavaDoc;
15
16 import java.rmi.RemoteException JavaDoc;
17
18 import javax.ejb.SessionBean JavaDoc;
19 import javax.ejb.SessionContext JavaDoc;
20 import javax.ejb.EJBException JavaDoc;
21 import javax.ejb.CreateException JavaDoc;
22 import javax.ejb.FinderException JavaDoc;
23 import javax.ejb.RemoveException JavaDoc;
24
25 import javax.naming.Context JavaDoc;
26 import javax.naming.InitialContext JavaDoc;
27 import javax.naming.NamingException JavaDoc;
28
29 import javax.rmi.PortableRemoteObject JavaDoc;
30
31 import org.apache.log4j.Category;
32
33 /**
34  * Manager session bean.
35  *
36  * @ejb.bean
37  * type="Stateless"
38  * name="M2MBManager"
39  * jndi-name="M2MBManagerEJB.M2MBManagerHome"
40  * generate="true"
41  * view-type="remote"
42  *
43  * @ejb.ejb-ref
44  * ejb-name="Project"
45  * view-type="local"
46  * @ejb.ejb-ref
47  * ejb-name="Developer"
48  * view-type="local"
49  *
50  * @ejb.transaction type="Required"
51  */

52 public class M2MBManagerBean
53    implements SessionBean JavaDoc
54 {
55    // Attributes --------------------------------------------------
56
static Category log = Category.getInstance( M2MBManagerBean.class );
57
58    static String JavaDoc PROJECT_NAME = "java:comp/env/ejb/Project";
59    static String JavaDoc DEVELOPER_NAME = "java:comp/env/ejb/Developer";
60
61    private ProjectLocalHome projectHome;
62    private DeveloperLocalHome developerHome;
63
64    // Business methods ---------------------------------------------
65
/**
66     * Creates a project
67     *
68     * @ejb.interface-method
69     */

70    public void createProject( String JavaDoc projectName )
71    {
72       try
73       {
74          projectHome.create( projectName );
75       }
76       catch( CreateException JavaDoc ce )
77       {
78          throw new EJBException JavaDoc( ce );
79       }
80    }
81
82    /**
83     * Creates a developer
84     *
85     * @ejb.interface-method
86     */

87    public void createDeveloper( String JavaDoc developerName )
88    {
89       try
90       {
91          developerHome.create( developerName );
92       }
93       catch( CreateException JavaDoc ce )
94       {
95          throw new EJBException JavaDoc( ce );
96       }
97    }
98
99    /**
100     * Returns developers for project
101     *
102     * @ejb.interface-method
103     */

104    public Collection JavaDoc getDevelopersForProject( String JavaDoc projectName )
105    {
106       try
107       {
108          ProjectLocal project = projectHome.findByPrimaryKey( projectName );
109          Collection JavaDoc devs = new ArrayList JavaDoc();
110          for(Iterator JavaDoc iter=project.getDevelopers().iterator(); iter.hasNext();)
111          {
112             DeveloperLocal developer = (DeveloperLocal) iter.next();
113             devs.add( developer.getName() );
114          }
115          return devs;
116       }
117       catch( FinderException JavaDoc fe )
118       {
119         throw new EJBException JavaDoc( fe );
120       }
121    }
122
123    /**
124     * Returns projects for developer
125     *
126     * @ejb.interface-method
127     */

128    public Collection JavaDoc getProjectsForDeveloper( String JavaDoc developerName )
129    {
130       try
131       {
132          DeveloperLocal developer = developerHome.findByPrimaryKey( developerName );
133          Collection JavaDoc prjs = new ArrayList JavaDoc();
134          for(Iterator JavaDoc iter=developer.getProjects().iterator(); iter.hasNext();)
135          {
136             ProjectLocal project = (ProjectLocal) iter.next();
137             prjs.add( project.getName() );
138          }
139          return prjs;
140       }
141       catch( FinderException JavaDoc fe )
142       {
143         throw new EJBException JavaDoc( fe );
144       }
145    }
146
147
148    /**
149     * Adds a project to developer
150     *
151     * @ejb.interface-method
152     */

153    public void addProjectToDeveloper( String JavaDoc developerName,
154                                       String JavaDoc projectName )
155    {
156       try
157       {
158          DeveloperLocal dev = developerHome.findByPrimaryKey( developerName );
159          ProjectLocal prj = projectHome.findByPrimaryKey( projectName );
160          dev.getProjects().add( prj );
161       }
162       catch( Exception JavaDoc e )
163       {
164         throw new EJBException JavaDoc( e );
165       }
166    }
167
168    /**
169     * Adds a develeloper to project
170     *
171     * @ejb.interface-method
172     */

173    public void addDeveloperToProject( String JavaDoc projectName,
174                                       String JavaDoc developerName )
175    {
176       try
177       {
178          DeveloperLocal dev = developerHome.findByPrimaryKey( developerName );
179          ProjectLocal prj = projectHome.findByPrimaryKey( projectName );
180          prj.getDevelopers().add( dev );
181       }
182       catch( Exception JavaDoc e )
183       {
184         throw new EJBException JavaDoc( e );
185       }
186    }
187
188    /**
189     * Removes project if exists
190     *
191     * @ejb.interface-method
192     */

193    public void removeProjectIfExists( String JavaDoc projectName )
194    {
195       try
196       {
197          ProjectLocal project = projectHome.findByPrimaryKey( projectName );
198          project.remove();
199       }
200       catch( Exception JavaDoc e )
201       {
202          // yam-yam
203
}
204    }
205
206    /**
207     * Removes developer if exists
208     *
209     * @ejb.interface-method
210     */

211    public void removeDeveloperIfExists( String JavaDoc developerName )
212    {
213       try
214       {
215          DeveloperLocal developer = developerHome.findByPrimaryKey( developerName );
216          developer.remove();
217       }
218       catch( Exception JavaDoc e )
219       {
220          // yam-yam
221
}
222    }
223
224
225    // SessionBean implementation -------------------------------------
226

227    public void setSessionContext(SessionContext JavaDoc c)
228    {
229       try
230       {
231          Context JavaDoc ic = new InitialContext JavaDoc();
232          developerHome = (DeveloperLocalHome)ic.lookup(DEVELOPER_NAME);
233          projectHome = (ProjectLocalHome)ic.lookup(PROJECT_NAME);
234       }
235       catch(NamingException JavaDoc ne)
236       {
237          throw new EJBException JavaDoc(ne);
238       }
239    }
240
241    /**
242     * create method
243     *
244     * @ejb:create-method
245     */

246    public void ejbCreate() { }
247    public void ejbActivate() { }
248    public void ejbPassivate() { }
249    public void ejbRemove() { }
250 }
251
Popular Tags