001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)RegistryUtil.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: /**
030: * GenericQueryImpl.java
031: *
032: * SUN PROPRIETARY/CONFIDENTIAL.
033: * This software is the proprietary information of Sun Microsystems, Inc.
034: * Use is subject to license terms.
035: *
036: * Created on August 23, 2006, 5:17 PM
037: */package com.sun.jbi.management.registry.xml;
038:
039: import java.util.List;
040: import java.util.logging.Logger;
041:
042: import com.sun.jbi.ComponentInfo;
043: import com.sun.jbi.ComponentQuery;
044: import com.sun.jbi.ComponentType;
045: import com.sun.jbi.ServiceAssemblyInfo;
046: import com.sun.jbi.ServiceAssemblyState;
047: import com.sun.jbi.ServiceAssemblyQuery;
048: import com.sun.jbi.ServiceUnitInfo;
049: import com.sun.jbi.StringTranslator;
050: import com.sun.jbi.management.LocalStringKeys;
051: import com.sun.jbi.management.registry.Registry;
052: import com.sun.jbi.management.registry.Updater;
053: import com.sun.jbi.management.registry.RegistryException;
054: import com.sun.jbi.management.repository.RepositoryException;
055: import com.sun.jbi.management.repository.ArchiveType;
056: import com.sun.jbi.management.system.ManagementContext;
057:
058: /**
059: * Utility class used by the registry implementation
060: *
061: * @author Sun Microsystems, Inc.
062: */
063: public class RegistryUtil {
064: private static final String DOMAIN = "domain";
065:
066: private RegistryImpl mRegistry;
067: private ManagementContext mMgtCtx;
068: private StringTranslator mTranslator;
069: private Logger mLog;
070:
071: public RegistryUtil(ManagementContext mgtCtx, RegistryImpl registry) {
072: mRegistry = registry;
073: mMgtCtx = mgtCtx;
074: mLog = mgtCtx.getLogger();
075: mTranslator = mgtCtx.getEnvironmentContext()
076: .getStringTranslator("com.sun.jbi.management");
077: }
078:
079: /**
080: * The components, shared libraries and service assemblies registered in the registry
081: * should be present in the repository and vice versa. Any entity outside the
082: * intersection set of the registry/repository will be deleted.
083: *
084: *
085: */
086: synchronized public void syncWithRepository() throws Exception {
087: String readOnly = mRegistry
088: .getProperty(Registry.REGISTRY_READONLY_PROPERTY);
089: if (!Boolean.parseBoolean(readOnly)) {
090: syncEntity(ArchiveType.SERVICE_ASSEMBLY);
091: syncEntity(ArchiveType.COMPONENT);
092: cleanOrphanedServiceAssemblies();
093: syncEntity(ArchiveType.SHARED_LIBRARY);
094: }
095: }
096:
097: /**
098: * Cleans up an entity from the registry and/or the repository.
099: *
100: * If a service assembly is registered in the repository and not in the registry
101: * then the service assembly is removed from the domain entry as well as any other
102: * installed entry.
103: * @param type of entity
104: * @param name of the entity
105: * @param isInRegistry indication
106: * @param inInReposity indication
107: * @throws RegistryException if there is any registry related error
108: * @throws RepositoryException if there is any repository related error
109: */
110: public void cleanEntity(ArchiveType type, String name,
111: boolean isInRegistry, boolean isInRepository)
112: throws RegistryException, RepositoryException {
113: String missingFromReg;
114: String missingFromRepos;
115: String removingFromReg;
116: String removingFromRepos;
117:
118: missingFromReg = LocalStringKeys.JBI_ADMIN_SHARED_LIBRARY_MISSING_IN_REGISTRY;
119: removingFromReg = LocalStringKeys.JBI_ADMIN_REMOVING_SHARED_LIBRARY_FROM_REGISTRY;
120: missingFromRepos = LocalStringKeys.JBI_ADMIN_SHARED_LIBRARY_MISSING_IN_REPOSITORY;
121: removingFromRepos = LocalStringKeys.JBI_ADMIN_REMOVING_SHARED_LIBRARY_FROM_REPOSITORY;
122: if (type.equals(ArchiveType.COMPONENT)) {
123: missingFromReg = LocalStringKeys.JBI_ADMIN_COMPONENT_MISSING_IN_REGISTRY;
124: removingFromReg = LocalStringKeys.JBI_ADMIN_REMOVING_COMPONENT_FROM_REGISTRY;
125: missingFromRepos = LocalStringKeys.JBI_ADMIN_COMPONENT_MISSING_IN_REPOSITORY;
126: removingFromRepos = LocalStringKeys.JBI_ADMIN_REMOVING_COMPONENT_FROM_REPOSITORY;
127: } else if (type.equals(ArchiveType.SERVICE_ASSEMBLY)) {
128: missingFromReg = LocalStringKeys.JBI_ADMIN_SERVICE_ASSEMBLY_MISSING_IN_REGISTRY;
129: removingFromReg = LocalStringKeys.JBI_ADMIN_REMOVING_SERVICE_ASSEMBLY_FROM_REGISTRY;
130: missingFromRepos = LocalStringKeys.JBI_ADMIN_SERVICE_ASSEMBLY_MISSING_IN_REPOSITORY;
131: removingFromRepos = LocalStringKeys.JBI_ADMIN_REMOVING_SERVICE_ASSEMBLY_FROM_REPOSITORY;
132: }
133:
134: if (!isInRegistry && isInRepository) {
135: mLog.warning(mTranslator.getString(missingFromReg, name));
136: mLog.info(mTranslator.getString(removingFromRepos, name));
137: mMgtCtx.getRepository().removeArchive(type, name);
138: }
139:
140: if (isInRegistry && !isInRepository) {
141: mLog.warning(mTranslator.getString(missingFromRepos, name));
142: mLog.info(mTranslator.getString(removingFromReg, name));
143:
144: Updater updater = mRegistry.getUpdater();
145: List<String> serversAndClusters;
146: (serversAndClusters = mRegistry.getGenericQuery()
147: .getServers()).addAll(mRegistry.getGenericQuery()
148: .getClusters());
149:
150: if (type.equals(ArchiveType.SHARED_LIBRARY)) {
151: for (String sc : serversAndClusters) {
152: updater.removeSharedLibrary(sc, name);
153: }
154: updater.removeSharedLibrary(DOMAIN, name);
155: } else if (type.equals(ArchiveType.COMPONENT)) {
156: for (String sc : serversAndClusters) {
157: updater.removeComponent(sc, name);
158: }
159: updater.removeComponent(DOMAIN, name);
160: } else if (type.equals(ArchiveType.SERVICE_ASSEMBLY)) {
161: for (String sc : serversAndClusters) {
162: ComponentQuery compQuery = mRegistry
163: .getComponentQuery(sc);
164:
165: List<String> comps = compQuery
166: .getComponentIds(ComponentType.BINDINGS_AND_ENGINES);
167: for (String comp : comps) {
168: ComponentInfo compInfo = compQuery
169: .getComponentInfo(comp);
170: removeServiceAssemblyFromComponent(sc,
171: compInfo, name);
172: }
173: updater.removeServiceAssembly(sc, name);
174: }
175: updater.removeServiceAssembly(DOMAIN, name);
176: }
177: }
178: }
179:
180: /*----------------------------------------------------------------------------------*\
181: * Private Helpers *
182: \*----------------------------------------------------------------------------------*/
183:
184: private void removeServiceAssemblyFromComponent(String target,
185: ComponentInfo compInfo, String saName)
186: throws RegistryException {
187: List<ServiceUnitInfo> suInfos = compInfo.getServiceUnitList();
188: Updater updater = mRegistry.getUpdater();
189:
190: for (ServiceUnitInfo suInfo : suInfos) {
191: if (saName.equals(suInfo.getServiceAssemblyName())) {
192: updater.removeServiceUnitFromComponent(target, compInfo
193: .getName(), suInfo.getName());
194: }
195: }
196: }
197:
198: /**
199: * Get the list of entities of type ArchiveType registered in the registry and in the repository,
200: * any entity outside the intersection set of these two lists is deleted.
201: */
202: private void syncEntity(ArchiveType type) throws Exception {
203: List<String> registeredEntities = null;
204:
205: if (type.equals(ArchiveType.COMPONENT)) {
206: registeredEntities = mRegistry.getGenericQuery()
207: .getRegisteredComponents();
208: } else if (type.equals(ArchiveType.SERVICE_ASSEMBLY)) {
209: registeredEntities = mRegistry.getGenericQuery()
210: .getRegisteredServiceAssemblies();
211: } else if (type.equals(ArchiveType.SHARED_LIBRARY)) {
212: registeredEntities = mRegistry.getGenericQuery()
213: .getRegisteredSharedLibraries();
214: }
215:
216: List<String> repositoryEntities = mMgtCtx.getRepository()
217: .getArchiveEntityNames(type);
218:
219: for (String entity : registeredEntities) {
220: if (!(repositoryEntities.contains(entity))) {
221: // -- Remove the entity from the registry
222: cleanEntity(type, entity, true, false);
223: }
224: repositoryEntities.remove(entity);
225: }
226:
227: // -- If the repositoryEntities list is non-empty, these are entities which
228: // -- are in the repository only, clean the repository
229: for (String entity : repositoryEntities) {
230: // -- Remove the entity from the repository
231: cleanEntity(type, entity, false, true);
232: }
233: }
234:
235: /**
236: * If after a service assembly is deployed on an instance / cluster, *all* target
237: * components are wiped out, the service assembly reference in the server/cluster
238: * ref is orphaned. If this is not a assembly with zero service units, then this
239: * service assembly reference needs to be cleaned otherwise the registry puts the
240: * JBI runtime in a bad state.
241: *
242: * @exception on any errors
243: */
244: private void cleanOrphanedServiceAssemblies() throws Exception {
245: // Get all the targets
246: List<String> serversAndClusters;
247: (serversAndClusters = mRegistry.getGenericQuery().getServers())
248: .addAll(mRegistry.getGenericQuery().getClusters());
249:
250: // Look for orphaned service assemblies on each target, cleanup if one found
251: for (String sc : serversAndClusters) {
252: cleanOrphanedServiceAssemblies(sc);
253: }
254: }
255:
256: /**
257: * Clean up any orphaned service assemblies from this target
258: *
259: * @exception on errors
260: */
261: private void cleanOrphanedServiceAssemblies(String target)
262: throws Exception {
263: ServiceAssemblyQuery saQuery = mRegistry
264: .getServiceAssemblyQuery(target);
265:
266: List<String> sas = saQuery.getServiceAssemblies();
267: for (String sa : sas) {
268: ServiceAssemblyInfo saInfo = saQuery
269: .getServiceAssemblyInfo(sa);
270:
271: if (saInfo.getStatus() == ServiceAssemblyState.UNKNOWN) {
272: // Clean this orphaned service assembly
273: mRegistry.getUpdater()
274: .removeServiceAssembly(target, sa);
275: }
276: }
277: }
278: }
|