KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > olstore > framework > EJBHomeFactory


1 /**
2  * Copyright (c) 2004 Red Hat, Inc. All rights reserved.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA
18  *
19  * Component of: Red Hat Application Server
20  *
21  * Initial Developers: Aizaz Ahmed
22  * Vivek Lakshmanan
23  * Andrew Overholt
24  * Matthew Wringe
25  *
26  */

27 package olstore.framework;
28
29
30 import java.util.Collections;
31 import java.util.HashMap;
32 import java.util.Map;
33
34 import javax.ejb.EJBLocalHome;
35 import javax.naming.Context;
36 import javax.naming.InitialContext;
37 import javax.naming.NamingException;
38
39 import olstore.entity.AddressUtil;
40 import olstore.entity.FriendUtil;
41 import olstore.entity.ItemUtil;
42 import olstore.entity.OrderUtil;
43 import olstore.entity.PictureUtil;
44 import olstore.entity.PropertyUtil;
45 import olstore.entity.TypeUtil;
46 import olstore.entity.UserUtil;
47 import olstore.session.ShoppingCartUtil;
48 import olstore.session.helper.ItemHelperUtil;
49 import olstore.session.helper.TypeHelperUtil;
50 import olstore.session.helper.UserHelperUtil;
51
52 /**
53  * Used to get the local homes of the entity and session beans.
54  */

55 public class EJBHomeFactory {
56     private Map homes;
57     private static EJBHomeFactory factory;
58     private Context context;
59     
60     private static final String NAMESPACE = "java:comp/env/ejb/";
61     
62     public static final String ITEM = NAMESPACE + "ItemLocal";
63     public static final String ITEM_HELPER = NAMESPACE + "ItemHelperLocal";
64     public static final String TYPE_HELPER = NAMESPACE + "TypeHelperLocal";
65     public static final String PROP = NAMESPACE + "PropertyLocal";
66     public static final String PIC = NAMESPACE + "PictureLocal";
67     public static final String TYPE = NAMESPACE + "TypeLocal";
68     public static final String USER = NAMESPACE + "UserLocal";
69     public static final String FRIENDS = NAMESPACE + "FriendLocal";
70     public static final String ORDER = NAMESPACE + "OrderLocal";
71     public static final String ADDRESS=NAMESPACE + "AddressLocal";
72     
73     public static final String USER_HELPER= NAMESPACE+"UserHelperLocal";
74     public static final String SHOPPINGCART= NAMESPACE+ "ShoppingCartLocal";
75     
76     
77     
78     /**
79      * Initialize the factory, cache the context and prepare the home cache
80      */

81     private EJBHomeFactory() throws NamingException {
82         homes = Collections.synchronizedMap ( new HashMap() );
83         context = new InitialContext ();
84     }
85     
86     /**
87      * Use this method to get an instance of the factory
88      */

89     public static EJBHomeFactory getInstance() throws NamingException {
90         if ( factory == null ) {
91             factory = new EJBHomeFactory();
92         }
93         return factory;
94     }
95     
96     /**
97      * This is designed only for local home interfaces.
98      */

99     public EJBLocalHome getLocalHome ( String jndiName ) throws NamingException {
100         EJBLocalHome home = null;
101         if(jndiName.equals(ITEM))
102             home = (EJBLocalHome) ItemUtil.getLocalHome();
103         if(jndiName.equals(ITEM_HELPER))
104             home = (EJBLocalHome) ItemHelperUtil.getLocalHome();
105         if(jndiName.equals(TYPE_HELPER))
106             home = (EJBLocalHome) TypeHelperUtil.getLocalHome();
107         if(jndiName.equals(PROP))
108             home = (EJBLocalHome) PropertyUtil.getLocalHome();
109         if(jndiName.equals(PIC))
110             home = (EJBLocalHome) PictureUtil.getLocalHome();
111         if(jndiName.equals(TYPE))
112             home = (EJBLocalHome) TypeUtil.getLocalHome();
113         if(jndiName.equals(USER))
114             home = (EJBLocalHome) UserUtil.getLocalHome();
115         if(jndiName.equals(FRIENDS))
116             home = (EJBLocalHome) FriendUtil.getLocalHome();
117         if(jndiName.equals(ORDER))
118             home = (EJBLocalHome) OrderUtil.getLocalHome();
119         if(jndiName.equals(ADDRESS))
120             home = (EJBLocalHome) AddressUtil.getLocalHome();
121         if(jndiName.equals(USER_HELPER))
122             home = (EJBLocalHome) UserHelperUtil.getLocalHome();
123         if(jndiName.equals(SHOPPINGCART))
124             home = (EJBLocalHome) ShoppingCartUtil.getLocalHome();
125         
126         //home = (EJBLocalHome)homes.get ( jndiName );
127
//if ( home == null ) {
128
// home = (EJBLocalHome) context.lookup ( jndiName );
129
// homes.put ( jndiName, home );
130
//}
131
return home;
132     }
133 }
134
Popular Tags