001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.foedeployer.ejb.m2mb;
023:
024: import java.sql.Date;
025: import java.util.Collection;
026: import java.util.HashSet;
027: import java.util.Set;
028: import java.util.Iterator;
029: import java.util.ArrayList;
030:
031: import java.rmi.RemoteException;
032:
033: import javax.ejb.SessionBean;
034: import javax.ejb.SessionContext;
035: import javax.ejb.EJBException;
036: import javax.ejb.CreateException;
037: import javax.ejb.FinderException;
038: import javax.ejb.RemoveException;
039:
040: import javax.naming.Context;
041: import javax.naming.InitialContext;
042: import javax.naming.NamingException;
043:
044: import javax.rmi.PortableRemoteObject;
045:
046: import org.apache.log4j.Category;
047:
048: /**
049: * Manager session bean.
050: *
051: * @ejb.bean
052: * type="Stateless"
053: * name="M2MBManager"
054: * jndi-name="M2MBManagerEJB.M2MBManagerHome"
055: * generate="true"
056: * view-type="remote"
057: *
058: * @ejb.ejb-ref
059: * ejb-name="Project"
060: * view-type="local"
061: * @ejb.ejb-ref
062: * ejb-name="Developer"
063: * view-type="local"
064: *
065: * @ejb.transaction type="Required"
066: */
067: public class M2MBManagerBean implements SessionBean {
068: // Attributes --------------------------------------------------
069: static Category log = Category.getInstance(M2MBManagerBean.class);
070:
071: static String PROJECT_NAME = "java:comp/env/ejb/Project";
072: static String DEVELOPER_NAME = "java:comp/env/ejb/Developer";
073:
074: private ProjectLocalHome projectHome;
075: private DeveloperLocalHome developerHome;
076:
077: // Business methods ---------------------------------------------
078: /**
079: * Creates a project
080: *
081: * @ejb.interface-method
082: */
083: public void createProject(String projectName) {
084: try {
085: projectHome.create(projectName);
086: } catch (CreateException ce) {
087: throw new EJBException(ce);
088: }
089: }
090:
091: /**
092: * Creates a developer
093: *
094: * @ejb.interface-method
095: */
096: public void createDeveloper(String developerName) {
097: try {
098: developerHome.create(developerName);
099: } catch (CreateException ce) {
100: throw new EJBException(ce);
101: }
102: }
103:
104: /**
105: * Returns developers for project
106: *
107: * @ejb.interface-method
108: */
109: public Collection getDevelopersForProject(String projectName) {
110: try {
111: ProjectLocal project = projectHome
112: .findByPrimaryKey(projectName);
113: Collection devs = new ArrayList();
114: for (Iterator iter = project.getDevelopers().iterator(); iter
115: .hasNext();) {
116: DeveloperLocal developer = (DeveloperLocal) iter.next();
117: devs.add(developer.getName());
118: }
119: return devs;
120: } catch (FinderException fe) {
121: throw new EJBException(fe);
122: }
123: }
124:
125: /**
126: * Returns projects for developer
127: *
128: * @ejb.interface-method
129: */
130: public Collection getProjectsForDeveloper(String developerName) {
131: try {
132: DeveloperLocal developer = developerHome
133: .findByPrimaryKey(developerName);
134: Collection prjs = new ArrayList();
135: for (Iterator iter = developer.getProjects().iterator(); iter
136: .hasNext();) {
137: ProjectLocal project = (ProjectLocal) iter.next();
138: prjs.add(project.getName());
139: }
140: return prjs;
141: } catch (FinderException fe) {
142: throw new EJBException(fe);
143: }
144: }
145:
146: /**
147: * Adds a project to developer
148: *
149: * @ejb.interface-method
150: */
151: public void addProjectToDeveloper(String developerName,
152: String projectName) {
153: try {
154: DeveloperLocal dev = developerHome
155: .findByPrimaryKey(developerName);
156: ProjectLocal prj = projectHome
157: .findByPrimaryKey(projectName);
158: dev.getProjects().add(prj);
159: } catch (Exception e) {
160: throw new EJBException(e);
161: }
162: }
163:
164: /**
165: * Adds a develeloper to project
166: *
167: * @ejb.interface-method
168: */
169: public void addDeveloperToProject(String projectName,
170: String developerName) {
171: try {
172: DeveloperLocal dev = developerHome
173: .findByPrimaryKey(developerName);
174: ProjectLocal prj = projectHome
175: .findByPrimaryKey(projectName);
176: prj.getDevelopers().add(dev);
177: } catch (Exception e) {
178: throw new EJBException(e);
179: }
180: }
181:
182: /**
183: * Removes project if exists
184: *
185: * @ejb.interface-method
186: */
187: public void removeProjectIfExists(String projectName) {
188: try {
189: ProjectLocal project = projectHome
190: .findByPrimaryKey(projectName);
191: project.remove();
192: } catch (Exception e) {
193: // yam-yam
194: }
195: }
196:
197: /**
198: * Removes developer if exists
199: *
200: * @ejb.interface-method
201: */
202: public void removeDeveloperIfExists(String developerName) {
203: try {
204: DeveloperLocal developer = developerHome
205: .findByPrimaryKey(developerName);
206: developer.remove();
207: } catch (Exception e) {
208: // yam-yam
209: }
210: }
211:
212: // SessionBean implementation -------------------------------------
213:
214: public void setSessionContext(SessionContext c) {
215: try {
216: Context ic = new InitialContext();
217: developerHome = (DeveloperLocalHome) ic
218: .lookup(DEVELOPER_NAME);
219: projectHome = (ProjectLocalHome) ic.lookup(PROJECT_NAME);
220: } catch (NamingException ne) {
221: throw new EJBException(ne);
222: }
223: }
224:
225: /**
226: * create method
227: *
228: * @ejb:create-method
229: */
230: public void ejbCreate() {
231: }
232:
233: public void ejbActivate() {
234: }
235:
236: public void ejbPassivate() {
237: }
238:
239: public void ejbRemove() {
240: }
241: }
|