-
Notifications
You must be signed in to change notification settings - Fork 3
/
AbstractServiceProvider.java
77 lines (68 loc) · 2.59 KB
/
AbstractServiceProvider.java
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
/*******************************************************************************
* Copyright (c) 2015 Obeo.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.acceleo.query.runtime.impl;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.acceleo.query.runtime.IReadOnlyQueryEnvironment;
import org.eclipse.acceleo.query.runtime.IService;
import org.eclipse.acceleo.query.runtime.IServiceProvider;
import org.eclipse.acceleo.query.runtime.ServiceUtils;
/**
* {@link IServiceProvider} scanning its own public methods to create {@link IService}.
*
* @author <a href="mailto:yvan.lussaud@obeo.fr">Yvan Lussaud</a>
*/
public abstract class AbstractServiceProvider implements IServiceProvider {
/**
* Wrong implementation of this {@link Class} message.
*/
private static final String WRONG_IMPLEMENTATION = "Wrong implementation of "
+ AbstractServiceProvider.class.getName();
/**
* {@link List} of {@link IService}.
*/
private List<IService<?>> services;
@Override
public List<IService<?>> getServices(IReadOnlyQueryEnvironment queryEnvironment, boolean forWorkspace) {
try {
if (services == null) {
final Method getServicesMethod = getClass().getMethod("getServices",
IReadOnlyQueryEnvironment.class, boolean.class);
services = new ArrayList<IService<?>>();
final Method[] methods = this.getClass().getMethods();
for (Method method : methods) {
if (ServiceUtils.isServiceMethod(this, method) && !getServicesMethod.equals(method)) {
final IService<Method> service = getService(method, forWorkspace);
if (service != null) {
services.add(service);
}
}
}
}
} catch (NoSuchMethodException e) {
throw new IllegalStateException(WRONG_IMPLEMENTATION, e);
} catch (SecurityException e) {
throw new IllegalStateException(WRONG_IMPLEMENTATION, e);
}
return services;
}
/**
* Gets an {@link IService} for the given {@link Method}.
*
* @param method
* the {@link Method}
* @return the {@link IService} if any, <code>null</code> if no {@link IService} correspond to the given
* {@link Method}
* @since 8.0.2
*/
protected abstract IService<Method> getService(Method method, boolean forWorkspace);
}