Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 56429c7951e004d46608fe0f9a69c4fa8074836d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*******************************************************************************
 * Copyright (c) 2005, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.ds.parser;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;

import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.eclipse.equinox.ds.Activator;
import org.eclipse.equinox.ds.Log;
import org.eclipse.osgi.util.ManifestElement;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.ServiceReference;
import org.osgi.service.component.ComponentConstants;
import org.osgi.util.tracker.ServiceTracker;
import org.osgi.util.tracker.ServiceTrackerCustomizer;
import org.xml.sax.SAXException;

/**
 * 
 * Parse the component description xml
 * 
 * @version $Revision: 1.3 $
 */
public class Parser implements ServiceTrackerCustomizer {

	private Activator main;

	/* ServiceTracker for parser */
	private ServiceTracker parserTracker;

	/**
	 * If an XML Parser is needed but not available, wait until one becomes available
	 */
	private List delayedParseBundles = new ArrayList();

	/**
	 * Create and open a ServiceTracker which will track registered XML parsers
	 * 
	 * @param main Main object.
	 */
	public Parser(Activator main) {
		this.main = main;
		parserTracker = new ServiceTracker(main.context, ParserConstants.SAX_FACTORY_CLASS, this);
		parserTracker.open();
	}

	public void dispose() {
		parserTracker.close();
	}

	public List getComponentDescriptions(BundleContext bundleContext) throws XMLParserNotAvailableException {
		ManifestElement[] xml = parseManifestHeader(bundleContext.getBundle());

		List result;

		if (xml != null) {
			result = new ArrayList(xml.length);
			for (int i = 0; i < xml.length; i++) {
				List components = parseComponentDescription(bundleContext, xml[i].getValue());
				result.addAll(components);
			}
		} else {
			result = Collections.EMPTY_LIST;
		}

		return result;
	}

	/*
	 * Get the xml files from the bundle
	 * 
	 * @param bundle Bundle @return Vector holding all the xmlfiles for the
	 * specifed Bundle
	 */
	private ManifestElement[] parseManifestHeader(Bundle bundle) {

		Dictionary headers = bundle.getHeaders();
		String files = (String) headers.get(ComponentConstants.SERVICE_COMPONENT);

		try {
			return ManifestElement.parseHeader(ComponentConstants.SERVICE_COMPONENT, files);
		} catch (BundleException e) {
			Log.log(1, "[SCR] Error attempting parse Manifest Element Header. ", e);
			return new ManifestElement[0];
		}
	}

	/**
	 * Given the bundle and the xml filename, parse it!
	 * 
	 * @param bundle Bundle
	 * @param xml String
	 */
	private List parseComponentDescription(BundleContext bundleContext, String xml) throws XMLParserNotAvailableException {

		SAXParserFactory parserFactory = (SAXParserFactory) parserTracker.getService();
		if (parserFactory == null) {
			// backup to using jaxp to create a new instance
			try {
				parserFactory = SAXParserFactory.newInstance();
			} catch (FactoryConfigurationError err) {
				/* whoops - we need an XML parser but we don't have one - put bundle on
				 * "delayed parse list" and raise an exception
				 */
				delayedParseBundles.add(bundleContext.getBundle());
				throw new XMLParserNotAvailableException();
			}
		}

		List result = new ArrayList();
		int fileIndex = xml.lastIndexOf('/');
		String path = fileIndex != -1 ? xml.substring(0, fileIndex) : "/";
		try {
			Enumeration urls = bundleContext.getBundle().findEntries(path, xml.substring(fileIndex + 1), false);
			if (urls == null || !urls.hasMoreElements()) {
				throw new BundleException("resource not found: " + xml);
			}
			URL url = (URL) urls.nextElement();
			InputStream is = url.openStream();
			parserFactory.setNamespaceAware(true);
			parserFactory.setValidating(false);
			SAXParser saxParser = parserFactory.newSAXParser();
			saxParser.parse(is, new ParserHandler(bundleContext, result));
		} catch (IOException e) {
			Log.log(1, "[SCR] IOException attempting to parse ComponentDescription XML. ", e);
		} catch (BundleException e) {
			Log.log(1, "[SCR] BundleException attempting to parse ComponentDescription XML. ", e);
		} catch (SAXException e) {
			Log.log(1, "[SCR] SAXException attempting to parse ComponentDescription XML. ", e);
		} catch (ParserConfigurationException e) {
			Log.log(1, "[SCR] ParserConfigurationException attempting to parse ComponentDescription XML. ", e);
		}

		return result;
	}

	/**
	 * If we are waiting for the XML parser to appear, kick the main bundle loop to 
	 * process
	 */
	public Object addingService(ServiceReference reference) {

		Object xmlParser = main.context.getService(reference);

		Iterator delayedParseBundlesItr = delayedParseBundles.iterator();
		while (delayedParseBundlesItr.hasNext()) {
			Bundle bundle = (Bundle) delayedParseBundlesItr.next();
			delayedParseBundlesItr.remove();
			if (bundle.getState() == Bundle.ACTIVE) {
				main.addingBundle(bundle);
			}
		}

		return xmlParser;
	}

	public void modifiedService(ServiceReference reference, Object object) {
		//nothing
	}

	public void removedService(ServiceReference reference, Object object) {
		main.context.ungetService(reference);
	}
}

Back to the top