KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > resource > PersistenceManagerFactoryResourceDeployer


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * Copyright 2000-2001 by iPlanet/Sun Microsystems, Inc.,
26  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
27  * All rights reserved.
28  *
29  * This software is the confidential and proprietary information
30  * of iPlanet/Sun Microsystems, Inc. ("Confidential Information").
31  * You shall not disclose such Confidential Information and shall
32  * use it only in accordance with the terms of the license
33  * agreement you entered into with iPlanet/Sun Microsystems.
34  *
35  * $Id: PersistenceManagerFactoryResourceDeployer.java,v 1.3 2005/12/25 04:15:57 tcfujii Exp $
36  */

37 package com.sun.enterprise.resource;
38
39 import com.sun.enterprise.server.ResourceDeployer;
40 import com.sun.enterprise.config.serverbeans.PersistenceManagerFactoryResource;
41 import com.sun.enterprise.config.serverbeans.Resources;
42 import com.sun.enterprise.config.serverbeans.JdbcResource;
43 import com.sun.enterprise.config.ConfigBean;
44 import com.sun.enterprise.connectors.ConnectorAdminServiceUtils;
45 import com.sun.enterprise.connectors.ConnectorConstants;
46
47 import com.sun.enterprise.Switch;
48 import com.sun.enterprise.NamingManager;
49 import com.sun.enterprise.resource.ResourceInstaller;
50 import com.sun.enterprise.repository.IASJ2EEResourceFactoryImpl;
51 import com.sun.enterprise.repository.J2EEResource;
52 import com.sun.enterprise.repository.PMFResource;
53
54 import java.util.logging.Logger JavaDoc;
55 import java.util.logging.Level JavaDoc;
56 import com.sun.logging.LogDomains;
57 import com.sun.enterprise.util.i18n.StringManager;
58
59 import javax.naming.InitialContext JavaDoc;
60
61 /**
62  * Handles PersistenceManagerFactory resource evnets in the server instance.
63  * When user adds a pmf resource, the admin instance fires a resource event.
64  * And the event is propagated to this object.
65  * The methods may be invoked concurrently, therefore synchronized is used.
66  *
67  * @author Shing Wai Chan
68  */

69
70 public class PersistenceManagerFactoryResourceDeployer implements ResourceDeployer {
71
72     /** StringManager for this deployer */
73     private static final StringManager localStrings =
74         StringManager.getManager("com.sun.enterprise.resource");
75
76     /** logger for this deployer */
77     private static Logger JavaDoc _logger=LogDomains.getLogger(LogDomains.CORE_LOGGER);
78
79     //---- begin implements ResourceDeployer ----
80

81     public synchronized void deployResource(Object JavaDoc resource) throws Exception JavaDoc {
82         PersistenceManagerFactoryResource configPMFRes =
83                 (PersistenceManagerFactoryResource)resource;
84
85         if (configPMFRes.isEnabled()) {
86             // load associated jdbc resource with PMF
87
loadJdbcResource(configPMFRes);
88
89             PMFResource j2eeResource = (PMFResource)
90                 IASJ2EEResourceFactoryImpl.toPMFJ2EEResource(configPMFRes);
91
92             ResourceInstaller installer =
93                 Switch.getSwitch().getResourceInstaller();
94             installer.installPersistenceManagerResource(j2eeResource);
95
96             installer.addResource(j2eeResource);
97         } else {
98             _logger.log(Level.INFO, "core.resource_disabled",
99                 new Object JavaDoc[] {configPMFRes.getJndiName(),
100                               IASJ2EEResourceFactoryImpl.PMF_RES_TYPE});
101         }
102     }
103
104     public synchronized void undeployResource(Object JavaDoc resource)
105             throws Exception JavaDoc {
106         NamingManager namingMgr = Switch.getSwitch().getNamingManager();
107         PersistenceManagerFactoryResource configPMFRes =
108                 (PersistenceManagerFactoryResource)resource;
109         namingMgr.unpublishObject(configPMFRes.getJndiName());
110
111         ResourceInstaller installer = Switch.getSwitch().getResourceInstaller();
112         installer.removeResource((PMFResource)
113                 IASJ2EEResourceFactoryImpl.toPMFJ2EEResource(configPMFRes));
114     }
115
116     public synchronized void redeployResource(Object JavaDoc resource)
117             throws Exception JavaDoc {
118         undeployResource(resource);
119         deployResource(resource);
120     }
121
122     public synchronized void enableResource(Object JavaDoc resource) throws Exception JavaDoc {
123         deployResource(resource);
124     }
125
126     public synchronized void disableResource(Object JavaDoc resource) throws Exception JavaDoc {
127         undeployResource(resource);
128     }
129
130     /**
131      * Utility method to find a resource from a Resource bean and convert
132      * it to a resource object to be used by ResourceDeployer implementation
133      *
134      * @param name resource name (normally the jndi-name)
135      * @param rbeans Resources config-beans
136      * @exception Exception thrown if fail
137      */

138     public Object JavaDoc getResource(String JavaDoc name, Resources rbeans) throws Exception JavaDoc {
139         Object JavaDoc res = rbeans.getPersistenceManagerFactoryResourceByJndiName(name);
140         if (res == null) {
141             String JavaDoc msg = localStrings.getString("resource.no_resource",name);
142             throw new Exception JavaDoc(msg);
143         }
144         return res;
145     }
146     
147     private void loadJdbcResource(PersistenceManagerFactoryResource cr)
148                         throws Exception JavaDoc {
149
150         String JavaDoc resName = cr.getJdbcResourceJndiName();
151         Resources resources = (Resources) cr.parent();
152         ConfigBean cb = resources.getJdbcResourceByJndiName(resName);
153         if (cb != null) {
154             try {
155                 InitialContext JavaDoc ic = new InitialContext JavaDoc();
156                 ic.lookup(resName);
157             } catch (Exception JavaDoc e) {
158                 // resource is not loaded
159
JdbcResourceDeployer deployer =
160                     new JdbcResourceDeployer();
161                 deployer.deployResource(
162                 (com.sun.enterprise.config.serverbeans.JdbcResource) cb);
163             }
164         }
165     }
166
167     //---- end implements ResourceDeployer ----
168
}
169
Popular Tags