-
Notifications
You must be signed in to change notification settings - Fork 10
/
generateApplicationBinder.mtl
215 lines (187 loc) · 9.07 KB
/
generateApplicationBinder.mtl
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
212
213
214
215
[comment encoding = UTF-8 /]
[comment
/*
* Copyright (c) 2020 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Eclipse Distribution License 1.0
* which is available at http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Simple
*/
/]
[module generateApplicationBinder('http://org.eclipse.lyo/oslc4j/adaptorInterface')]
[import org::eclipse::lyo::oslc4j::codegenerator::services::services/]
[import org::eclipse::lyo::oslc4j::codegenerator::services::serviceServices/]
[import org::eclipse::lyo::oslc4j::codegenerator::services::adaptorInterfaceServices/]
[import org::eclipse::lyo::oslc4j::codegenerator::services::storeServices/]
[template public generateClassHeader(anAdaptorInterface : AdaptorInterface)]
// [protected ('Copyright')]
/*
* Copyright (c) 2020 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Simple
*
* This file is generated by Lyo Designer (https://www.eclipse.org/lyo/)
*/
// [/protected]
[/template]
[template public generateImports(anAdaptorInterface : AdaptorInterface)]
import java.util.ArrayList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.glassfish.hk2.api.Factory;
import org.glassfish.hk2.utilities.binding.AbstractBinder;
import javax.inject.Singleton;
import [anAdaptorInterface.javaClassFullNameForAdaptorManager()/];
import [anAdaptorInterface.javaClassFullNameForResourcesFactory()/];
[if (anAdaptorInterface.storeEnabled())][anAdaptorInterface.storeImportsForBinder()/][/if]
[if (anAdaptorInterface.trsServiceEnabled())
]import org.eclipse.lyo.oslc4j.trs.server.InmemPagedTrs;
import org.eclipse.lyo.oslc4j.trs.server.PagedTrs;
import org.eclipse.lyo.oslc4j.trs.server.PagedTrsFactory;
import org.eclipse.lyo.oslc4j.trs.server.TrsEventHandler;
[/if]
import org.eclipse.lyo.oslc4j.core.OSLC4JUtils;
// [protected ('imports')]
// [/protected]
[/template]
[template public generateConfigureMethod(anAdaptorInterface : AdaptorInterface) post (trim())]
@Override
protected void configure() {
log.info("HK2 contract binding start");
bindAsContract([anAdaptorInterface.javaClassNameForAdaptorManager()/].class).in(Singleton.class);
bindFactory(new ResourcesFactoryFactory()).to([anAdaptorInterface.javaClassNameForResourcesFactory()/].class).in(Singleton.class);
[if (anAdaptorInterface.storeEnabled())]
bindFactory(new StorePoolFactory()).to(StorePool.class).in(Singleton.class);
[/if]
[if (anAdaptorInterface.trsServiceEnabled())]
// [protected ('TRSInitialise')]
// [/protected]
ArrayList<URI> uris = new ArrayList<URI>();
// [protected ('TRSInitialBase')]
//TODO: Provide the initial list of URIs to populate the TRS log with
// [/protected]
InmemPagedTrs inmemTrs = new PagedTrsFactory().getInmemPagedTrs([anAdaptorInterface.trsService.basePageLimit/], [anAdaptorInterface.trsService.changeLogPageLimit/], uris);
bind(inmemTrs).to(TrsEventHandler.class);
bind(inmemTrs).to(PagedTrs.class);
// [protected ('TRSFinalize')]
// [/protected]
[/if]
}
[/template]
[template public generateResourcesFactoryFactory(anAdaptorInterface : AdaptorInterface) post (trim())]
private final class ResourcesFactoryFactory implements Factory<[anAdaptorInterface.javaClassNameForResourcesFactory()/]> {
@Override
public [anAdaptorInterface.javaClassNameForResourcesFactory()/] provide() {
return new [anAdaptorInterface.javaClassNameForResourcesFactory()/](OSLC4JUtils.getServletURI());
}
@Override
public void dispose([anAdaptorInterface.javaClassNameForResourcesFactory()/] instance) {
}
}
[/template]
[template public generateStorePoolFactory(anAdaptorInterface : AdaptorInterface) post (trim())]
[if (anAdaptorInterface.storeEnabled())]
private final class StorePoolFactory implements Factory<StorePool> {
private Optional<String> getPropertyFromEnvironment(String envKey) {
final Map<String, String> env = System.getenv();
if(env.containsKey(envKey)) {
log.info("Found env variable with key {}", envKey);
return Optional.of(env.get(envKey));
}
return Optional.empty();
}
private Optional<String> getPropertyFromFile(Properties properties, String key) {
if(properties.containsKey(key)) {
log.info("Found property with key {}", key);
return Optional.of(properties.getProperty(key));
}
return Optional.empty();
}
private StorePool initializeStorePool() {
//For each store configuration setting, first try to load it from an environment variable.
//If no such variable exists, try to load from the properties file - if such a file exists.
//Raise an exception of any of the necessary settings are missing from both the environment and properties file.
Properties lyoStoreProperties = new Properties();
URL lyoStorePropertiesFile = [anAdaptorInterface.javaClassNameForApplicationBinder()/].class.getResource("[anAdaptorInterface.relativeFileNameForStoreProperties()/]");
try {
if (null != lyoStorePropertiesFile) {
lyoStoreProperties.load(new FileInputStream(lyoStorePropertiesFile.getFile()));
}
else {
log.info("Failed to read properties file for Store configuration. Hopefully, settings are available via Environment variables.");
}
} catch (FileNotFoundException | SecurityException e) {
log.info("Failed to read properties file for Store configuration. Hopefully, settings are available via Environment variables.", e);
}
catch (IOException e) {
log.error("Failed to initialize Store. properties file for Store configuration could not be loaded.", e);
throw new RuntimeException(e);
}
int initialPoolSize = Integer.parseInt(getPropertyFromEnvironment("LYO_STORE_INITIAL_POOL_SIZE")
.orElseGet(() -> getPropertyFromFile(lyoStoreProperties, "initialPoolSize").orElseThrow()));
URI defaultNamedGraph = URI.create(getPropertyFromEnvironment("LYO_STORE_DEFAULT_NAMED_GRAPH")
.orElseGet(() -> getPropertyFromFile(lyoStoreProperties, "defaultNamedGraph").orElseThrow()));
URI sparqlQueryEndpoint = URI.create(getPropertyFromEnvironment("LYO_STORE_SPARQL_QUERY_ENDPOINT")
.orElseGet(() -> getPropertyFromFile(lyoStoreProperties, "sparqlQueryEndpoint").orElseThrow()));
URI sparqlUpdateEndpoint = URI.create(getPropertyFromEnvironment("LYO_STORE_SPARQL_UPDATE_ENDPOINT")
.orElseGet(() -> getPropertyFromFile(lyoStoreProperties, "sparqlUpdateEndpoint").orElseThrow()));
String userName = null;
String password = null;
[if (anAdaptorInterface.oneAndOnlyStore().oclIsTypeOf(StoreWithAuthentication))]
userName = lyoStoreProperties.getProperty("username");
password = lyoStoreProperties.getProperty("password");
[/if]
[anAdaptorInterface.javaClassNameForStorePool()/] storePool = new [anAdaptorInterface.javaClassNameForStorePool()/](initialPoolSize, defaultNamedGraph, sparqlQueryEndpoint, sparqlUpdateEndpoint, userName, password);
return storePool;
}
@Override
public StorePool provide() {
// [protected ('StoreInitialise')]
// [/protected]
StorePool storePool = initializeStorePool();
// [protected ('StoreFinalize')]
// [/protected]
return storePool;
}
@Override
public void dispose(StorePool instance) {
}
}
[/if]
[/template]
[template public generateApplicationBinder(anAdaptorInterface : AdaptorInterface)]
[file (javaClassFullFileNameForApplicationBinder(anAdaptorInterface), false, 'UTF-8')]
[generateClassHeader(anAdaptorInterface)/]
package [javaClassPackageNameForApplicationBinder(anAdaptorInterface) /];
[generateImports(anAdaptorInterface)/]
// [protected ('pre_class_code')]
// [/protected]
public class [javaClassNameForApplicationBinder(anAdaptorInterface) /] extends AbstractBinder {
private static final Logger log = LoggerFactory.getLogger([javaClassNameForApplicationBinder(anAdaptorInterface)/].class);
// [protected ('class_attributes')]
// [/protected]
// [protected ('class_methods')]
// [/protected]
public [javaClassNameForApplicationBinder(anAdaptorInterface)/]()
{
log.info("HK2 contract binding init");
}
[generateConfigureMethod(anAdaptorInterface)/]
[generateResourcesFactoryFactory(anAdaptorInterface)/]
[generateStorePoolFactory(anAdaptorInterface)/]
}
[/file]
[/template]