001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.catalog;
019:
020: import java.io.IOException;
021: import java.net.URL;
022: import java.util.Collections;
023: import java.util.Enumeration;
024: import java.util.HashSet;
025: import java.util.Set;
026: import java.util.logging.Level;
027: import java.util.logging.Logger;
028:
029: import javax.annotation.PostConstruct;
030: import javax.annotation.Resource;
031:
032: import org.apache.cxf.Bus;
033: import org.apache.xml.resolver.Catalog;
034: import org.apache.xml.resolver.CatalogManager;
035: import org.apache.xml.resolver.tools.CatalogResolver;
036:
037: public class OASISCatalogManager {
038: public static final String DEFAULT_CATALOG_NAME = "META-INF/jax-ws-catalog.xml";
039:
040: private static final Logger LOG = Logger
041: .getLogger(OASISCatalogManager.class.getName());
042:
043: private Catalog resolver;
044: private Set<URL> loadedCatalogs = Collections
045: .synchronizedSet(new HashSet<URL>());
046: private Bus bus;
047:
048: public OASISCatalogManager() {
049: CatalogManager catalogManager = new CatalogManager();
050: catalogManager.setUseStaticCatalog(false);
051: catalogManager.setIgnoreMissingProperties(true);
052: CatalogResolver catalogResolver = new CatalogResolver(
053: catalogManager);
054: this .resolver = catalogResolver.getCatalog();
055: }
056:
057: public Bus getBus() {
058: return bus;
059: }
060:
061: @Resource
062: public void setBus(Bus bus) {
063: this .bus = bus;
064: }
065:
066: @PostConstruct
067: public void register() {
068: if (null != bus) {
069: bus.setExtension(this , OASISCatalogManager.class);
070: }
071: loadContextCatalogs();
072: }
073:
074: public Catalog getCatalog() {
075: return this .resolver;
076: }
077:
078: public void loadContextCatalogs() {
079: loadContextCatalogs(DEFAULT_CATALOG_NAME);
080: }
081:
082: public void loadContextCatalogs(String name) {
083: try {
084: loadCatalogs(
085: Thread.currentThread().getContextClassLoader(),
086: name);
087: } catch (IOException e) {
088: LOG.log(Level.WARNING, "Error loading " + name
089: + " catalog files", e);
090: }
091: }
092:
093: public void loadCatalogs(ClassLoader classLoader, String name)
094: throws IOException {
095: if (classLoader == null) {
096: return;
097: }
098:
099: Enumeration<URL> catalogs = classLoader.getResources(name);
100: while (catalogs.hasMoreElements()) {
101: URL catalogURL = catalogs.nextElement();
102: if (!loadedCatalogs.contains(catalogURL)) {
103: this .resolver.parseCatalog(catalogURL);
104: loadedCatalogs.add(catalogURL);
105: }
106: }
107: }
108:
109: public void loadCatalog(URL catalogURL) throws IOException {
110: if (!loadedCatalogs.contains(catalogURL)) {
111: this .resolver.parseCatalog(catalogURL);
112: loadedCatalogs.add(catalogURL);
113: }
114: }
115:
116: private static OASISCatalogManager getContextCatalog() {
117: OASISCatalogManager oasisCatalog = new OASISCatalogManager();
118: oasisCatalog.loadContextCatalogs();
119: return oasisCatalog;
120: }
121:
122: public static OASISCatalogManager getCatalogManager(Bus bus) {
123: if (bus == null) {
124: return getContextCatalog();
125: }
126: OASISCatalogManager catalog = bus
127: .getExtension(OASISCatalogManager.class);
128: if (catalog == null) {
129: return getContextCatalog();
130: } else {
131: return catalog;
132: }
133: }
134:
135: }
|