KickJava   Java API By Example, From Geeks To Geeks.

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


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  * @(#) MailResourceDeployer.java
26  *
27  * Copyright 2002 by iPlanet/Sun Microsystems, Inc.,
28  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
29  * All rights reserved.
30  */

31 package com.sun.enterprise.resource;
32
33 import com.sun.enterprise.server.ResourceDeployer;
34 import com.sun.enterprise.config.serverbeans.Resources;
35
36 import com.sun.enterprise.NamingManager;
37 import com.sun.enterprise.resource.ResourceInstaller;
38 import com.sun.enterprise.repository.J2EEResource;
39 import com.sun.enterprise.repository.MailResource;
40 import com.sun.enterprise.repository.IASJ2EEResourceFactoryImpl;
41
42 import java.util.logging.Logger JavaDoc;
43 import java.util.logging.Level JavaDoc;
44 import com.sun.logging.LogDomains;
45 import com.sun.enterprise.util.i18n.StringManager;
46 import com.sun.enterprise.ManagementObjectManager;
47
48 /**
49  * Handles mail resource events in the server instance.
50  *
51  * The mail resource events from the admin instance are propagated
52  * to this object.
53  *
54  * The methods can potentially be called concurrently, therefore implementation
55  * need to be synchronized.
56  *
57  * @author James Kong
58  * @since JDK1.4
59  */

60 public class MailResourceDeployer extends GlobalResourceDeployer
61         implements ResourceDeployer {
62
63     /** StringManager for this deployer */
64     private static final StringManager localStrings =
65         StringManager.getManager("com.sun.enterprise.resource");
66
67     /** logger for this deployer */
68     private static Logger JavaDoc _logger=LogDomains.getLogger(LogDomains.CORE_LOGGER);
69
70     /**
71      * Deploy the resource into the server's runtime naming context
72      *
73      * @param resoure a resource object (eg. MailResource)
74      * @exception Exception thrown if fail
75      */

76     public synchronized void deployResource(Object JavaDoc resource) throws Exception JavaDoc {
77
78         com.sun.enterprise.config.serverbeans.MailResource mailRes =
79             (com.sun.enterprise.config.serverbeans.MailResource) resource;
80
81         if(mailRes == null) {
82             _logger.log(Level.INFO, "core.resourcedeploy_error");
83         } else {
84             if (mailRes.isEnabled()) {
85                 //registers the jsr77 object for the mail resource deployed
86
ManagementObjectManager mgr =
87                     getAppServerSwitchObject().getManagementObjectManager();
88                 mgr.registerJavaMailResource(mailRes.getJndiName());
89                 installResource(mailRes);
90             } else {
91                 _logger.log(Level.INFO, "core.resource_disabled",
92                         new Object JavaDoc[] {mailRes.getJndiName(),
93                         IASJ2EEResourceFactoryImpl.MAIL_RES_TYPE});
94             }
95         }
96     }
97
98     /**
99      * Local method for calling the ResourceInstaller for installing
100      * mail resource in runtime.
101      *
102      * @param resource The mail resource to be installed.
103      */

104     void installResource(com.sun.enterprise.config.serverbeans.MailResource
105             mailResource) throws Exception JavaDoc {
106         // Converts the config data to j2ee resource ;
107
// retieves the resource installer ; installs the resource ;
108
// and adds it to a collection in the installer
109
J2EEResource j2eeRes =
110                 IASJ2EEResourceFactoryImpl.toMailJ2EEResource(mailResource);
111         ResourceInstaller installer =
112             getAppServerSwitchObject().getResourceInstaller();
113         installer.installMailResource((MailResource) j2eeRes);
114         installer.addResource(j2eeRes);
115     }
116     
117     /**
118      * Undeploy the resource from the server's runtime naming context
119      *
120      * @param resoure a resource object (eg. MailResource)
121      * @exception Exception thrown if fail
122      */

123     public synchronized void undeployResource(Object JavaDoc resource)
124             throws Exception JavaDoc {
125
126         // naming manager - provides jndi support
127
NamingManager namingMgr = getAppServerSwitchObject().getNamingManager();
128
129         com.sun.enterprise.config.serverbeans.MailResource mailRes =
130             (com.sun.enterprise.config.serverbeans.MailResource) resource;
131
132         // converts the config data to j2ee resource
133
J2EEResource j2eeResource =
134             IASJ2EEResourceFactoryImpl.toMailJ2EEResource(mailRes);
135
136         // removes the resource from jndi naming
137
namingMgr.unpublishObject(j2eeResource.getName());
138
139         // resource installer
140
ResourceInstaller installer = getAppServerSwitchObject().getResourceInstaller();
141
142         // removes the resource from the collection
143
installer.removeResource(j2eeResource);
144         
145         ManagementObjectManager mgr =
146                 getAppServerSwitchObject().getManagementObjectManager();
147         mgr.unregisterJavaMailResource(mailRes.getJndiName());
148     }
149
150     /**
151      * Redeploy the resource into the server's runtime naming context
152      *
153      * @param resoure a resource object (eg. MailResource)
154      * @exception Exception thrown if fail
155      */

156     public synchronized void redeployResource(Object JavaDoc resource)
157             throws Exception JavaDoc {
158
159         undeployResource(resource);
160         deployResource(resource);
161     }
162
163     /**
164      * Enable the resource in the server's runtime naming context
165      *
166      * @param resoure a resource object (eg. MailResource)
167      * @exception Exception thrown if fail
168      */

169     public synchronized void enableResource(Object JavaDoc resource) throws Exception JavaDoc {
170         deployResource(resource);
171     }
172
173     /**
174      * Disable the resource in the server's runtime naming context
175      *
176      * @param resoure a resource object (eg. MailResource)
177      * @exception Exception thrown if fail
178      */

179     public synchronized void disableResource(Object JavaDoc resource) throws Exception JavaDoc {
180         undeployResource(resource);
181     }
182
183
184     /**
185      * Utility method to find a resource from Resources beans and converte
186      * it to a resource object to be used by the implemented ResourceDeployer
187      *
188      * @param name resource name (normally the jndi-name)
189      * @param rbeans Resources config-beans
190      * @exception Exception thrown if fail
191      */

192     public Object JavaDoc getResource(String JavaDoc name, Resources rbeans) throws Exception JavaDoc {
193
194         Object JavaDoc res = rbeans.getMailResourceByJndiName(name);
195
196         if (res == null) {
197             String JavaDoc msg = localStrings.getString("resource.no_resource",name);
198             throw new Exception JavaDoc(msg);
199         }
200
201         return res;
202     }
203 }
204
Popular Tags