KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > server > core > jmx > storage > PersistenceChecker


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 package com.sun.enterprise.admin.server.core.jmx.storage;
25
26 //JMX imports
27
import javax.management.ObjectName JavaDoc;
28 import javax.management.InstanceNotFoundException JavaDoc;
29 //Admin imports
30
import com.sun.enterprise.admin.AdminContext;
31 import com.sun.enterprise.admin.common.ObjectNames;
32 import com.sun.enterprise.admin.common.ObjectNameHelper;
33 import com.sun.enterprise.admin.common.exception.MBeanConfigException;
34 import com.sun.enterprise.admin.common.constant.AdminConstants;
35 import com.sun.enterprise.admin.server.core.mbean.config.GenericConfigurator;
36 import com.sun.enterprise.admin.server.core.mbean.config.ServerController;
37 import com.sun.enterprise.admin.server.core.mbean.config.naming.ConfigMBeanNamingInfo;
38 //Other imports
39
import com.sun.enterprise.instance.ServerManager;
40 import com.sun.enterprise.instance.InstanceEnvironment;
41 import com.sun.enterprise.config.ConfigException;
42 import com.sun.enterprise.config.ConfigContext;
43 import com.sun.enterprise.config.ConfigFactory;
44 import com.sun.enterprise.config.ConfigBeansFactory;
45 import com.sun.enterprise.config.serverbeans.*;
46
47 import java.util.logging.Level JavaDoc;
48 import java.util.logging.Logger JavaDoc;
49
50 import com.sun.enterprise.admin.meta.MBeanRegistryFactory;
51 import com.sun.enterprise.admin.meta.MBeanRegistry;
52
53 /**
54     A class that acts as the bridge between persistent store and
55     MBean Repository. Uses the conversion between ObjectName and
56     identification into Config API and carries out the search.
57 */

58
59 public class PersistenceChecker
60 {
61     private static final Logger JavaDoc _logger = Logger.getLogger(
62             AdminConstants.kLoggerName);
63
64     private AdminContext mAdminContext;
65
66     /** Creates new PersistenceChecker */
67     public PersistenceChecker ()
68     {
69         //for future use.
70
}
71
72     public void setAdminContext(AdminContext ctx) {
73         mAdminContext = ctx;
74     }
75
76     private Object JavaDoc findElement_8_0(ObjectName JavaDoc objectName) throws InstanceNotFoundException JavaDoc
77     {
78         try
79         {
80             MBeanRegistry registry = MBeanRegistryFactory.getAdminMBeanRegistry();
81             return (Object JavaDoc)registry.findMBeanRegistryEntry(objectName);
82         }
83         catch(Exception JavaDoc e)
84         {
85             return null;
86         }
87     }
88     public Object JavaDoc findElement(ObjectName JavaDoc objectName) throws InstanceNotFoundException JavaDoc
89     {
90         Object JavaDoc match = null;
91         
92         match= findElement_8_0(objectName);
93         if(match!=null)
94             return match;
95         
96         //Now try the old 7.0 namespace
97
// which's still alive
98
String JavaDoc type = ObjectNameHelper.getType(objectName);
99         String JavaDoc instanceName = ObjectNameHelper.getServerInstanceName(objectName);
100         
101         if (type != null)
102         {
103             if (type.equals(ObjectNames.kController))
104             {
105                 match = new ServerController();
106             }
107             else if (type.equals(ObjectNames.kGenericConfigurator))
108             {
109                 match = new GenericConfigurator();
110             }
111             else if (type.equals(ObjectNames.kServerInstance))
112             {
113                 match = findServerInstance(instanceName);
114                 if(match==null)
115                     throw new InstanceNotFoundException JavaDoc(objectName.toString());
116             }
117             else if (type.equals(ObjectNames.kDeployment))
118             {
119                match = findServerInstance(instanceName);
120                if(match==null)
121                    throw new InstanceNotFoundException JavaDoc(objectName.toString());
122             }
123             else
124             {
125                 /* Ordinary Config Mbean - use ConfigMBeanNaming */
126                 match = findGenericConfigBean(objectName, instanceName);
127             }
128         }
129         else
130         {
131             /* unknown type */
132             _logger.log(Level.FINE, "mbean.config.admin.unknown_mbean_type", objectName.toString() );
133         }
134         return ( match );
135     }
136
137     private Object JavaDoc findServerInstance(String JavaDoc instanceName)
138     {
139         ServerManager sm = ServerManager.instance();
140     Server serverInstance = null;
141 /* boolean isAdminServer = instanceName.equals(
142                                     ServerManager.ADMINSERVER_ID); */

143         if (! sm.instanceExists(instanceName) /*&& !isAdminServer*/)
144         {
145             return null;
146         }
147
148         try
149         {
150             ConfigContext cc = getConfigContext(instanceName);
151             serverInstance = ServerBeansFactory.getServerBean(cc);
152         }
153         catch(Exception JavaDoc e)
154         {
155             e.printStackTrace(); //no harm in squelching
156
}
157         return ( serverInstance );
158     }
159     
160     private Object JavaDoc findGenericConfigBean(ObjectName JavaDoc objectName, String JavaDoc instanceName)
161     {
162         /* Ordinary Config Mbean - use ConfigMBeanNaming */
163         Object JavaDoc bean = null;
164         ConfigMBeanNamingInfo mbeanInfo = null;
165         try
166         {
167             mbeanInfo = new ConfigMBeanNamingInfo(objectName);
168         }
169         catch (MBeanConfigException mce)
170         {
171             _logger.log(Level.FINE, "mbean.config.admin.naming_not_found",
172                         new Object JavaDoc[]{objectName.toString(), mce.getLocalizedMessage()} );
173         }
174         if(mbeanInfo!=null)
175         {
176             String JavaDoc xPath = mbeanInfo.getXPath();
177             try
178             {
179                 ConfigContext ctx = getConfigContext(instanceName);
180                 bean = ConfigBeansFactory.getConfigBeanByXPath(ctx, xPath);
181             }
182             catch(Exception JavaDoc e)
183             {
184                 _logger.log(Level.FINE, "mbean.config.admin.config_bean_not_found",
185                             new Object JavaDoc[]{xPath, e.getLocalizedMessage()} );
186             }
187         }
188         return bean;
189     }
190     private ConfigContext getConfigContext(String JavaDoc instanceName) throws ConfigException
191     {
192         if (mAdminContext != null) {
193             return mAdminContext.getAdminConfigContext();
194         } else {
195             String JavaDoc backupServerXmlPath = new InstanceEnvironment(instanceName).getBackupConfigFilePath();
196             return (ConfigFactory.createConfigContext(backupServerXmlPath) );
197         }
198     }
199     
200 }
201
Popular Tags