Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 761dc98698e2f84881b02b018971d6c305196452 (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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*****************************************************************************
 * Copyright (c) CEA LIST.
 *
 *    
 * 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:
 *  Cedric Dumoulin  Cedric.dumoulin@lifl.fr - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.infra.core.services.internal;

import org.eclipse.core.runtime.Platform;
import org.eclipse.papyrus.infra.core.services.BadStateException;
import org.eclipse.papyrus.infra.core.services.ServiceDescriptor;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.core.services.ServiceState;
import org.eclipse.papyrus.infra.core.services.ServicesRegistry;
import org.osgi.framework.Bundle;

/**
 * Base class for the different types of service entries (PoJo, Service,
 * ServiceFactory, ...).
 * 
 * @author cedric dumoulin
 */
public abstract class ServiceTypeEntry {

	/**
	 * Current state of the service.
	 */
	protected ServiceState state = ServiceState.registered;

	/**
	 * Descriptor of the service associated to this entry.
	 */
	protected ServiceDescriptor serviceDescriptor;

	/**
	 * 
	 * Constructor.
	 * 
	 */
	public ServiceTypeEntry(ServiceDescriptor serviceDescriptor) {
		this.serviceDescriptor = serviceDescriptor;
	}

	/**
	 * Change the state of the service.
	 * 
	 * @param newState
	 */
	protected void setState(ServiceState newState) {
		state = newState;
	}

	/**
	 * @return the state
	 */
	public ServiceState getState() {
		return state;
	}

	/**
	 * Check if the current state is the proposed state. Throws an exception if
	 * the state is different.
	 * 
	 * @param expectedState
	 * @throws BadStateException
	 */
	protected void checkState(ServiceState expectedState) throws BadStateException {
		if(expectedState != state) {
			throw new BadStateException(expectedState, state, serviceDescriptor);
		}
	}

	/**
	 * Get the descriptor of the service associated to this entry.
	 * 
	 * @return
	 */
	public ServiceDescriptor getDescriptor() {
		return serviceDescriptor;
	}

	/**
	 * Instanciate the service as specified in serviceClassname.
	 * 
	 * @return the created service.
	 * @throws ServiceException
	 */
	protected Object instanciateService() throws ServiceException {

		// Load the Class of the service
		String serviceClassname = serviceDescriptor.getServiceClassname();
		Class<?> classname = loadClass();

		// Try to get the one arg constructor.
		// This require the ServiceRegistry. Old stuff, not more used.
		// try {
		// Constructor<?> constructor =
		// classname.getConstructor(ServicesRegistry.class);
		// return constructor.newInstance(registry);
		// } catch (SecurityException e) {
		// // Do nothing, try next constructor
		// } catch (NoSuchMethodException e) {
		// // Do nothing, try next constructor
		// } catch (IllegalArgumentException e) {
		// throw new ServiceException("Can't instanciate '" + serviceClassname +
		// "' with args ServicesRegistry.", e);
		// } catch (InstantiationException e) {
		// throw new ServiceException("Can't instanciate '" + serviceClassname +
		// "' with args ServicesRegistry.", e);
		// } catch (IllegalAccessException e) {
		// throw new ServiceException("Can't instanciate '" + serviceClassname +
		// "' with args ServicesRegistry.", e);
		// } catch (InvocationTargetException e) {
		// throw new ServiceException("Can't instanciate '" + serviceClassname +
		// "' with args ServicesRegistry.", e);
		// }

		// Try with zero arg constructor.
		try {
			return classname.newInstance();
		} catch (SecurityException e) {
			throw new ServiceException("Can't instanciate '" + serviceClassname + "' with zero args.", e); //$NON-NLS-1$ //$NON-NLS-2$
		} catch (IllegalArgumentException e) {
			throw new ServiceException("Can't instanciate '" + serviceClassname + "' with zero args.", e); //$NON-NLS-1$ //$NON-NLS-2$
		} catch (InstantiationException e) {
			throw new ServiceException("Can't instanciate '" + serviceClassname + "' with zero args.", e); //$NON-NLS-1$ //$NON-NLS-2$
		} catch (IllegalAccessException e) {
			throw new ServiceException("Can't instanciate '" + serviceClassname + "' with zero args.", e); //$NON-NLS-1$ //$NON-NLS-2$
		}
	}

	/**
	 * Load the Class object. Try from current ClassLoader, then try using the
	 * plugin referenced in the serviceDescriptor.PluginId
	 * 
	 * @return
	 * @throws ServiceException
	 */
	private Class<?> loadClass() throws ServiceException {
		String serviceClassname = serviceDescriptor.getServiceClassname();
		Class<?> serviceClass;
		try {
			serviceClass = Class.forName(serviceClassname);
		} catch (ClassNotFoundException e1) {
			// Try using bundle
			try {
				String bundleID = serviceDescriptor.getClassBundleID();
				Bundle bundle = Platform.getBundle(bundleID);
				serviceClass = bundle.loadClass(serviceClassname);
			} catch (ClassNotFoundException e2) {
				throw new ServiceException("Can't find class for the name '" + serviceClassname + "'.", e2); //$NON-NLS-1$ //$NON-NLS-2$
			}
		}

		return serviceClass;
	}

	/**
	 * Return true if the service is started. Return false otherwise.
	 * 
	 * @return
	 */
	public boolean isStarted() {
		return state == ServiceState.started;
	}

	/**
	 * Get the service instance.
	 * 
	 * @return
	 * @throws ServiceException
	 */
	abstract public Object getServiceInstance() throws ServiceException;

	/**
	 * Create the associated service if not a Lazy Service.
	 * 
	 * @throws ServiceException
	 */
	abstract public void createService() throws ServiceException;

	/**
	 * Start the associated service if not a Lazy Service.
	 * 
	 * @param servicesRegistry
	 *        The servicesRegistry containing this service.
	 * 
	 * @throws ServiceException
	 */
	abstract public void initService(ServicesRegistry servicesRegistry) throws ServiceException;

	/**
	 * Start the associated service if not a Lazy Service.
	 * 
	 * @throws ServiceException
	 */
	abstract public void startService() throws ServiceException;

	/**
	 * Dispose associated service.
	 */
	abstract public void disposeService() throws ServiceException;

}

Back to the top