use of org.apache.openejb.config.sys.ServiceProvider in project tomee by apache.
the class ProviderGenerator method main.
public static void main(final String[] args) throws Exception {
final ProviderManager manager = new ProviderManager(new ServiceJarXmlLoader());
final Set<String> seen = new HashSet<>();
final List<ServiceProvider> providers = manager.load("org.apache.tomee");
for (final ServiceProvider provider : providers) {
final List<String> types = provider.getTypes();
final String name = guessBuilder(types);
final String builder = name + "Builder";
if (seen.contains(builder)) {
continue;
}
seen.add(builder);
final String service = provider.getService();
final File file = new File("/Users/dblevins/work/all/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/typed/" + builder + ".java");
final OutputStream write = IO.write(file);
final PrintStream out = new PrintStream(write);
out.println("/*\n" + " * Licensed to the Apache Software Foundation (ASF) under one or more\n" + " * contributor license agreements. See the NOTICE file distributed with\n" + " * this work for additional information regarding copyright ownership.\n" + " * The ASF licenses this file to You under the Apache License, Version 2.0\n" + " * (the \"License\"); you may not use this file except in compliance with\n" + " * the License. You may obtain a copy of the License at\n" + " *\n" + " * http://www.apache.org/licenses/LICENSE-2.0\n" + " *\n" + " * Unless required by applicable law or agreed to in writing, software\n" + " * distributed under the License is distributed on an \"AS IS\" BASIS,\n" + " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" + " * See the License for the specific language governing permissions and\n" + " * limitations under the License.\n" + " */");
out.println("package org.apache.openejb.config.typed;");
out.println();
out.println("import org.apache.openejb.config.typed.util.*;");
out.println("import org.apache.openejb.config.sys.*;");
out.println("import javax.xml.bind.annotation.*;");
out.println("import " + Duration.class.getName() + ";");
out.println("import java.util.*;");
out.println("import java.util.concurrent.*;");
out.println("import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;");
out.println();
out.println(template("@XmlAccessorType(XmlAccessType.FIELD)\n" + "@XmlRootElement(name = \"${name}\")\n" + "public class ${builder} extends ${service} {\n").apply("builder", builder, "service", service, "name", name));
for (final Map.Entry<Object, Object> entry : provider.getProperties().entrySet()) {
final String key = Strings.lcfirst(entry.getKey().toString());
final String value = entry.getValue().toString();
final String type = guessType(key, value);
if (Duration.class.getName().endsWith(type)) {
out.println(" @XmlJavaTypeAdapter(DurationAdapter.class)");
}
out.println(template(" @XmlAttribute\n" + " private ${type} ${key} = ${value};").apply("builder", builder, "key", key, "value", asValue(type, value), "type", type));
}
out.println();
// Constructor
out.println(template(" public ${builder}() {\n" + " setClassName(\"${className}\");\n" + " setType(\"${type}\");\n" + " setId(\"${name}\");\n").apply("builder", builder, "className", String.valueOf(provider.getClassName()), "type", types.get(0), "name", name));
if (provider.getConstructor() != null) {
out.println(template(" setConstructor(\"${constructor}\");\n").apply("constructor", fixConstructor(provider)));
}
if (provider.getFactoryName() != null) {
out.println(template(" setFactoryName(\"${factoryName}\");\n").apply("factoryName", provider.getFactoryName()));
}
out.println(" }\n");
// Setters
out.println(template(" public ${builder} id(String id) {\n" + " setId(id);\n" + " return this;\n" + " }\n").apply("builder", builder));
for (final Map.Entry<Object, Object> entry : provider.getProperties().entrySet()) {
final String lcFirstKey = Strings.lcfirst(entry.getKey().toString());
final String ucFirstKey = Strings.ucfirst(lcFirstKey);
final String value = entry.getValue().toString();
final String type = guessType(lcFirstKey, value);
// builder method
out.println(template(" public ${builder} with${Key}(${type} ${key}) {\n" + " this.${key} = ${key};\n" + " return this;\n" + " }\n").apply("builder", builder, "key", lcFirstKey, "Key", ucFirstKey, "value", value, "type", type));
// setter
out.println(template(" public void set${Key}(${type} ${key}) {\n" + " this.${key} = ${key};\n" + " }\n").apply("key", lcFirstKey, "Key", ucFirstKey, "value", value, "type", type));
// getter
out.println(template(" public ${type} get${Key}() {\n" + " return ${key};\n" + " }\n").apply("key", lcFirstKey, "Key", ucFirstKey, "value", value, "type", type));
if (Duration.class.getName().equals(type)) {
out.println(template(" public ${builder} with${Key}(long time, TimeUnit unit) {\n" + " return with${Key}(new Duration(time, unit));\n" + " }\n").apply("builder", builder, "key", lcFirstKey, "Key", ucFirstKey, "value", value, "type", type));
out.println(template(" public void set${Key}(long time, TimeUnit unit) {\n" + " set${Key}(new Duration(time, unit));\n" + " }\n").apply("key", lcFirstKey, "Key", ucFirstKey, "value", value, "type", type));
}
final String s = lcFirstKey.toLowerCase(Locale.ENGLISH);
if ("long".equals(type) && s.contains("time")) {
TimeUnit unit = null;
if (s.endsWith("millis")) {
unit = TimeUnit.MILLISECONDS;
} else if (s.endsWith("milliseconds")) {
unit = TimeUnit.MILLISECONDS;
} else if (s.endsWith("seconds")) {
unit = TimeUnit.SECONDS;
} else if (s.endsWith("minutes")) {
unit = TimeUnit.MINUTES;
} else if (s.endsWith("hours")) {
unit = TimeUnit.HOURS;
}
if (unit == null) {
continue;
}
final Pattern pattern = Pattern.compile("(millis(econds)?|seconds|minutes|hours)", Pattern.CASE_INSENSITIVE);
final String lcFirstKey2 = pattern.matcher(lcFirstKey).replaceAll("");
final String ucFirstKey2 = pattern.matcher(ucFirstKey).replaceAll("");
out.println(template(" public ${builder} with${Key2}(long time, TimeUnit unit) {\n" + " return with${Key}(TimeUnit.${unit}.convert(time, unit));\n" + " }\n").apply("builder", builder, "key2", lcFirstKey2, "Key2", ucFirstKey2, "key", lcFirstKey, "Key", ucFirstKey, "value", value, "unit", unit.name(), "type", type));
out.println(template(" public void set${Key2}(long time, TimeUnit unit) {\n" + " set${Key}(TimeUnit.${unit}.convert(time, unit));\n" + " }\n").apply("key2", lcFirstKey2, "Key2", ucFirstKey2, "key", lcFirstKey, "Key", ucFirstKey, "value", value, "unit", unit.name(), "type", type));
}
}
out.println(" public Properties getProperties() {\n" + " return Builders.getProperties(this);\n" + " }\n");
out.println("}");
out.flush();
out.close();
}
}
use of org.apache.openejb.config.sys.ServiceProvider in project tomee by apache.
the class ProviderManagerTest method testInheritedAttributes.
public void testInheritedAttributes() throws Exception {
final ProviderManager manager = new ProviderManager(new ProviderLoader() {
@Override
public ServiceProvider load(final ID id) {
if ("color".equalsIgnoreCase(id.getName())) {
final ServiceProvider color = new ServiceProvider();
color.setClassName(Color.class.getName());
color.setFactoryName("fooFactory");
color.setId("Color");
color.setService("Resource");
color.setConstructor("one, two, three");
color.setDescription("the description");
color.setDisplayName("the display name");
color.getProperties().setProperty("red", "0");
color.getProperties().setProperty("green", "0");
color.getProperties().setProperty("blue", "0");
color.getTypes().add(Color.class.getName());
return color;
}
if ("red".equalsIgnoreCase(id.getName())) {
final ServiceProvider red = new ServiceProvider();
red.setId("Red");
red.setParent("Color");
red.getProperties().setProperty("red", "255");
return red;
}
if ("orange".equalsIgnoreCase(id.getName())) {
final ServiceProvider orange = new ServiceProvider();
orange.setId("Orange");
orange.setParent("Red");
orange.getProperties().setProperty("green", "200");
return orange;
}
throw new IllegalStateException(id.toString());
}
@Override
public List<ServiceProvider> load(final String namespace) {
final List<ServiceProvider> list = new ArrayList<>();
list.add(load(new ID(namespace, "color")));
list.add(load(new ID(namespace, "red")));
list.add(load(new ID(namespace, "orange")));
return list;
}
});
{
// Assert Orange
// Should have inherited from Red
final ServiceProvider provider = manager.get("dEFAUlT", "orAngE");
assertNotNull(provider);
assertEquals(Color.class.getName(), provider.getClassName());
assertEquals("Resource", provider.getService());
assertEquals("one, two, three", provider.getConstructor());
assertEquals("the description", provider.getDescription());
assertEquals("the display name", provider.getDisplayName());
assertEquals("fooFactory", provider.getFactoryName());
assertEquals("255", provider.getProperties().getProperty("reD"));
assertEquals("200", provider.getProperties().get("grEeN"));
assertEquals("0", provider.getProperties().get("bLue"));
assertEquals(1, provider.getTypes().size());
}
{
// Assert Red
// Should have inherited green and blue values from Color
final ServiceProvider provider = manager.get("dEFaulT", "REd");
assertNotNull(provider);
assertEquals(Color.class.getName(), provider.getClassName());
assertEquals("Resource", provider.getService());
assertEquals("one, two, three", provider.getConstructor());
assertEquals("the description", provider.getDescription());
assertEquals("the display name", provider.getDisplayName());
assertEquals("fooFactory", provider.getFactoryName());
assertEquals("255", provider.getProperties().getProperty("rED"));
assertEquals("0", provider.getProperties().get("grEEN"));
assertEquals("0", provider.getProperties().get("bLUe"));
}
{
// Assert Color
// Must be able to retrieve provider and properties in a case-insensitive manner
final ServiceProvider provider = manager.get("DeFaulT", "CoLoR");
assertNotNull(provider);
assertEquals(Color.class.getName(), provider.getClassName());
assertEquals("Resource", provider.getService());
assertEquals("one, two, three", provider.getConstructor());
assertEquals("the description", provider.getDescription());
assertEquals("the display name", provider.getDisplayName());
assertEquals("fooFactory", provider.getFactoryName());
assertEquals("0", provider.getProperties().getProperty("rEd"));
assertEquals("0", provider.getProperties().get("grEEn"));
assertEquals("0", provider.getProperties().get("blUE"));
}
assertEquals(3, manager.getAll().size());
}
use of org.apache.openejb.config.sys.ServiceProvider in project tomee by apache.
the class ProviderManagerTest method testLoader.
public void testLoader() throws Exception {
final ProviderManager manager = new ProviderManager(new ProviderLoader() {
@Override
public ServiceProvider load(final ID id) {
if ("color".equalsIgnoreCase(id.getName())) {
final ServiceProvider color = new ServiceProvider(Color.class, "Color", "Resource");
color.getProperties().setProperty("red", "0");
color.getProperties().setProperty("green", "0");
color.getProperties().setProperty("blue", "0");
return color;
}
if ("red".equalsIgnoreCase(id.getName())) {
final ServiceProvider red = new ServiceProvider();
red.setId("Red");
red.setParent("Color");
red.getProperties().setProperty("red", "255");
return red;
}
if ("orange".equalsIgnoreCase(id.getName())) {
final ServiceProvider orange = new ServiceProvider();
orange.setId("Orange");
orange.setParent("Red");
orange.getProperties().setProperty("green", "200");
return orange;
}
throw new IllegalStateException(id.toString());
}
@Override
public List<ServiceProvider> load(final String namespace) {
return null;
}
});
assertEquals(0, manager.getAll().size());
{
// Assert Orange
// Should have inherited from Red
final ServiceProvider provider = manager.get("dEFAUlT", "orAngE");
assertNotNull(provider);
assertEquals(Color.class.getName(), provider.getClassName());
assertEquals("255", provider.getProperties().getProperty("reD"));
assertEquals("200", provider.getProperties().get("grEeN"));
assertEquals("0", provider.getProperties().get("bLue"));
}
{
// Assert Red
// Should have inherited green and blue values from Color
final ServiceProvider provider = manager.get("dEFaulT", "REd");
assertNotNull(provider);
assertEquals(Color.class.getName(), provider.getClassName());
assertEquals("255", provider.getProperties().getProperty("rED"));
assertEquals("0", provider.getProperties().get("grEEN"));
assertEquals("0", provider.getProperties().get("bLUe"));
}
{
// Assert Color
// Must be able to retrieve provider and properties in a case-insensitive manner
final ServiceProvider provider = manager.get("DeFaulT", "CoLoR");
assertNotNull(provider);
assertEquals(Color.class.getName(), provider.getClassName());
assertEquals("0", provider.getProperties().getProperty("rEd"));
assertEquals("0", provider.getProperties().get("grEEn"));
assertEquals("0", provider.getProperties().get("blUE"));
}
assertEquals(3, manager.getAll().size());
}
use of org.apache.openejb.config.sys.ServiceProvider in project tomee by apache.
the class AutoConfigMdbContainerTest method _setUp.
protected void _setUp() throws Exception {
config = new ConfigurationFactory();
assembler = new Assembler();
assembler.createTransactionManager(config.configureService(TransactionServiceInfo.class));
assembler.createSecurityService(config.configureService(SecurityServiceInfo.class));
final ServiceProvider provider = new ServiceProvider(EmailResourceAdapter.class, EmailResourceAdapter.class.getSimpleName(), "Resource");
provider.getTypes().add(EmailResourceAdapter.class.getName());
ServiceUtils.getServiceProviders().add(provider);
}
use of org.apache.openejb.config.sys.ServiceProvider in project tomee by apache.
the class AppInfoBuilder method buildConnectorModules.
private void buildConnectorModules(final AppModule appModule, final AppInfo appInfo) throws OpenEJBException {
final String appId = appModule.getModuleId();
for (final ConnectorModule connectorModule : appModule.getConnectorModules()) {
//
// DEVELOPERS NOTE: if you change the id generation code here, you must change
// the id generation code in AutoConfig$AppResources
//
final Connector connector = connectorModule.getConnector();
final ConnectorInfo connectorInfo = new ConnectorInfo();
connectorInfo.description = connector.getDescription();
connectorInfo.displayName = connector.getDisplayName();
connectorInfo.path = connectorModule.getJarLocation();
connectorInfo.moduleId = connectorModule.getModuleId();
connectorInfo.watchedResources.addAll(connectorModule.getWatchedResources());
connectorInfo.validationInfo = ValidatorBuilder.getInfo(connectorModule.getValidationConfig());
connectorInfo.uniqueId = connectorModule.getUniqueId();
connectorInfo.mbeans = connectorModule.getMbeans();
final List<URL> libraries = connectorModule.getLibraries();
for (final URL url : libraries) {
final File file = toFile(url);
try {
connectorInfo.libs.add(file.getCanonicalPath());
} catch (final IOException e) {
throw new IllegalArgumentException("Invalid application lib path " + file.getAbsolutePath());
}
}
final ResourceAdapter resourceAdapter = connector.getResourceAdapter();
if (resourceAdapter.getResourceAdapterClass() != null) {
final String id = this.getId(connectorModule);
final String className = resourceAdapter.getResourceAdapterClass();
final ServiceProvider provider = new ServiceProvider(className, id, "Resource");
provider.getTypes().add(className);
ServiceUtils.registerServiceProvider(appId, provider);
final Resource resource = new Resource(id, className, appId + "#" + id);
for (final ConfigProperty property : resourceAdapter.getConfigProperty()) {
final String name = property.getConfigPropertyName();
final String value = property.getConfigPropertyValue();
if (value != null) {
resource.getProperties().setProperty(name, value);
}
}
connectorInfo.resourceAdapter = this.configFactory.configureService(resource, ResourceInfo.class);
}
final OutboundResourceAdapter outbound = resourceAdapter.getOutboundResourceAdapter();
if (outbound != null) {
String transactionSupport = "none";
final TransactionSupportType transactionSupportType = outbound.getTransactionSupport();
if (transactionSupportType != null) {
switch(transactionSupportType) {
case LOCAL_TRANSACTION:
transactionSupport = "local";
break;
case NO_TRANSACTION:
transactionSupport = "none";
break;
case XA_TRANSACTION:
transactionSupport = "xa";
break;
}
}
for (final ConnectionDefinition connection : outbound.getConnectionDefinition()) {
final String id = this.getId(connection, outbound, connectorModule);
final String className = connection.getManagedConnectionFactoryClass();
final String type = connection.getConnectionFactoryInterface();
final ServiceProvider provider = new ServiceProvider(className, id, "Resource");
provider.getTypes().add(type);
ServiceUtils.registerServiceProvider(appId, provider);
final Resource resource = new Resource(id, type, appId + "#" + id);
final Properties properties = resource.getProperties();
for (final ConfigProperty property : connection.getConfigProperty()) {
final String name = property.getConfigPropertyName();
final String value = property.getConfigPropertyValue();
if (value != null) {
properties.setProperty(name, value);
}
}
properties.setProperty("TransactionSupport", transactionSupport);
if (connectorInfo.resourceAdapter != null) {
properties.setProperty("ResourceAdapter", connectorInfo.resourceAdapter.id);
}
final ResourceInfo resourceInfo = this.configFactory.configureService(resource, ResourceInfo.class);
connectorInfo.outbound.add(resourceInfo);
}
}
final InboundResourceadapter inbound = resourceAdapter.getInboundResourceAdapter();
if (inbound != null) {
for (final MessageListener messageListener : inbound.getMessageAdapter().getMessageListener()) {
final String id = this.getId(messageListener, inbound, connectorModule);
final Container container = new Container(id, "MESSAGE", null);
final Properties properties = container.getProperties();
properties.setProperty("ResourceAdapter", connectorInfo.resourceAdapter.id);
properties.setProperty("MessageListenerInterface", messageListener.getMessageListenerType());
properties.setProperty("ActivationSpecClass", messageListener.getActivationSpec().getActivationSpecClass());
final MdbContainerInfo mdbContainerInfo = this.configFactory.configureService(container, MdbContainerInfo.class);
connectorInfo.inbound.add(mdbContainerInfo);
}
}
for (final AdminObject adminObject : resourceAdapter.getAdminObject()) {
final String id = this.getId(adminObject, resourceAdapter, connectorModule);
final String className = adminObject.getAdminObjectClass();
final String type = adminObject.getAdminObjectInterface();
final ServiceProvider provider = new ServiceProvider(className, id, "Resource");
provider.getTypes().add(type);
ServiceUtils.registerServiceProvider(appId, provider);
final Resource resource = new Resource(id, type, appId + "#" + id);
final Properties properties = resource.getProperties();
for (final ConfigProperty property : adminObject.getConfigProperty()) {
final String name = property.getConfigPropertyName();
final String value = property.getConfigPropertyValue();
if (value != null) {
properties.setProperty(name, value);
}
}
final ResourceInfo resourceInfo = this.configFactory.configureService(resource, ResourceInfo.class);
connectorInfo.adminObject.add(resourceInfo);
}
appInfo.connectors.add(connectorInfo);
}
}
Aggregations