use of org.apache.openejb.assembler.classic.OpenEjbConfiguration in project tomee by apache.
the class Info2Properties method main.
public static void main(final String[] args) {
final CommandLineParser parser = new PosixParser();
// create the Options
final Options options = new Options();
options.addOption(option("v", "version", "cmd.properties.opt.version"));
options.addOption(option("h", "help", "cmd.properties.opt.help"));
options.addOption(option("s", "server-url", "url", "cmd.properties.opt.server"));
CommandLine line = null;
try {
// parse the command line arguments
line = parser.parse(options, args);
} catch (final ParseException exp) {
help(options);
System.exit(-1);
}
if (line.hasOption("help")) {
help(options);
System.exit(0);
} else if (line.hasOption("version")) {
OpenEjbVersion.get().print(System.out);
System.exit(0);
}
final Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.RemoteInitialContextFactory");
final String serverUrl = line.getOptionValue("server-url", defaultServerUrl);
p.put(Context.PROVIDER_URL, serverUrl);
ConfigurationInfo configInfo = null;
try {
final InitialContext ctx = new InitialContext(p);
configInfo = (ConfigurationInfo) ctx.lookup("openejb/ConfigurationInfoBusinessRemote");
} catch (final ServiceUnavailableException e) {
System.out.println(e.getCause().getMessage());
System.out.println(messages.format("cmd.deploy.serverOffline"));
System.exit(1);
} catch (final NamingException e) {
System.out.println("ConfigurationInfo does not exist in server '" + serverUrl + "', check the server logs to ensure it exists and has not been removed.");
System.exit(2);
}
File tempFile = null;
try {
try {
tempFile = File.createTempFile("configrequest", "txt");
} catch (final Throwable e) {
final File tmp = new File("tmp");
if (!tmp.exists() && !tmp.mkdirs()) {
throw new IOException("Failed to create local tmp directory: " + tmp.getAbsolutePath());
}
tempFile = File.createTempFile("configrequest", "txt", tmp);
}
if (!tempFile.exists()) {
throw new IllegalStateException("Failed to create tmp file: " + tempFile.getAbsolutePath());
}
} catch (final Exception e) {
System.err.println("Temp file creation failed.");
e.printStackTrace();
System.exit(1);
}
OpenEjbConfiguration configuration = null;
try {
configuration = configInfo.getOpenEjbConfiguration(tempFile);
} catch (final ConfigurationInfo.UnauthorizedException e) {
System.err.println("This tool is currently crippled to only work with server's on the same physical machine. See this JIRA issue for details: http://issues.apache.org/jira/browse/OPENEJB-621");
System.exit(10);
}
printConfig(configuration);
}
use of org.apache.openejb.assembler.classic.OpenEjbConfiguration in project tomee by apache.
the class ConfigurationFactory method getContainerIds.
protected List<String> getContainerIds() {
final List<String> containerIds = new ArrayList<>();
final OpenEjbConfiguration runningConfig = getRunningConfig();
if (runningConfig != null) {
for (final ContainerInfo containerInfo : runningConfig.containerSystem.containers) {
containerIds.add(containerInfo.id);
}
}
if (sys != null) {
for (final ContainerInfo containerInfo : sys.containerSystem.containers) {
containerIds.add(containerInfo.id);
}
// the above sys instance
if (openejb != null) {
for (final Container container : openejb.getContainer()) {
containerIds.add(container.getId());
}
}
}
return containerIds;
}
use of org.apache.openejb.assembler.classic.OpenEjbConfiguration in project tomee by apache.
the class ConfigurationFactory method getContainerInfos.
protected List<ContainerInfo> getContainerInfos() {
final List<ContainerInfo> containers = new ArrayList<>();
final OpenEjbConfiguration runningConfig = getRunningConfig();
if (runningConfig != null) {
containers.addAll(runningConfig.containerSystem.containers);
}
if (sys != null) {
containers.addAll(sys.containerSystem.containers);
}
return containers;
}
use of org.apache.openejb.assembler.classic.OpenEjbConfiguration in project tomee by apache.
the class ApplicationComposers method startContainer.
public void startContainer(final Object instance) throws Exception {
originalProperties = (Properties) JavaSecurityManagers.getSystemProperties().clone();
originalLoader = Thread.currentThread().getContextClassLoader();
fixFakeClassFinder(instance);
// For the moment we just take the first @Configuration method
// maybe later we can add something fancy to allow multiple configurations using a qualifier
// as a sort of altDD/altConfig concept. Say for example the altDD prefix might be "foo",
// we can then imagine something like this:
// @Foo @Configuration public Properties alternateConfig(){...}
// @Foo @Module public Properties alternateModule(){...}
// anyway, one thing at a time ....
final Properties configuration = new Properties();
configuration.put(DEPLOYMENTS_CLASSPATH_PROPERTY, "false");
final EnableServices annotation = testClass.getAnnotation(EnableServices.class);
if (annotation != null && annotation.httpDebug()) {
configuration.setProperty("httpejbd.print", "true");
configuration.setProperty("httpejbd.indent.xml", "true");
configuration.setProperty("logging.level.OpenEJB.server.http", "FINE");
}
final org.apache.openejb.junit.EnableServices annotationOld = testClass.getAnnotation(org.apache.openejb.junit.EnableServices.class);
if (annotationOld != null && annotationOld.httpDebug()) {
configuration.setProperty("httpejbd.print", "true");
configuration.setProperty("httpejbd.indent.xml", "true");
configuration.setProperty("logging.level.OpenEJB.server.http", "FINE");
}
final WebResource webResource = testClass.getAnnotation(WebResource.class);
if (webResource != null && webResource.value().length > 0) {
configuration.setProperty("openejb.embedded.http.resources", Join.join(",", webResource.value()));
}
Openejb openejb = null;
final Map<Object, List<Method>> configs = new HashMap<>();
findAnnotatedMethods(configs, Configuration.class);
findAnnotatedMethods(configs, org.apache.openejb.junit.Configuration.class);
for (final Map.Entry<Object, List<Method>> method : configs.entrySet()) {
for (final Method m : method.getValue()) {
final Object o = m.invoke(method.getKey());
if (o instanceof Properties) {
final Properties properties = (Properties) o;
configuration.putAll(properties);
} else if (Openejb.class.isInstance(o)) {
openejb = Openejb.class.cast(o);
} else if (String.class.isInstance(o)) {
final String path = String.class.cast(o);
final URL url = Thread.currentThread().getContextClassLoader().getResource(path);
if (url == null) {
throw new IllegalArgumentException(o.toString() + " not found");
}
final InputStream in = url.openStream();
try {
if (path.endsWith(".json")) {
openejb = JSonConfigReader.read(Openejb.class, in);
} else {
openejb = JaxbOpenejb.readConfig(new InputSource(in));
}
} finally {
IO.close(in);
}
}
}
}
if (SystemInstance.isInitialized()) {
SystemInstance.reset();
}
Collection<String> propertiesToSetAgain = null;
final ContainerProperties configAnnot = testClass.getAnnotation(ContainerProperties.class);
if (configAnnot != null) {
for (final ContainerProperties.Property p : configAnnot.value()) {
final String value = p.value();
if (ContainerProperties.Property.IGNORED.equals(value)) {
// enforces some clean up since we can't set null in a hash table
System.clearProperty(p.name());
continue;
}
final String name = p.name();
configuration.put(name, value);
if (value.contains("${")) {
if (propertiesToSetAgain == null) {
propertiesToSetAgain = new LinkedList<>();
}
propertiesToSetAgain.add(name);
}
}
}
SystemInstance.init(configuration);
if (SystemInstance.get().getComponent(ThreadSingletonService.class) == null) {
CdiBuilder.initializeOWB();
}
for (final Map.Entry<Object, ClassFinder> finder : testClassFinders.entrySet()) {
for (final Field field : finder.getValue().findAnnotatedFields(RandomPort.class)) {
if (!field.isAccessible()) {
field.setAccessible(true);
}
final String service = field.getAnnotation(RandomPort.class).value();
final String key = ("http".equals(service) ? "httpejbd" : service) + ".port";
final String existing = SystemInstance.get().getProperty(key);
final int random;
if (existing == null) {
random = NetworkUtil.getNextAvailablePort();
SystemInstance.get().setProperty(key, Integer.toString(random));
} else {
random = Integer.parseInt(existing);
}
if (int.class == field.getType()) {
field.set(finder.getKey(), random);
} else if (URL.class == field.getType()) {
field.set(finder.getKey(), new URL("http://localhost:" + random + "/"));
}
}
}
for (final Map.Entry<Object, ClassFinder> finder : testClassFinders.entrySet()) {
if (!finder.getValue().findAnnotatedClasses(SimpleLog.class).isEmpty()) {
SystemInstance.get().setProperty("openejb.jul.forceReload", "true");
break;
}
}
final CdiExtensions cdiExtensions = testClass.getAnnotation(CdiExtensions.class);
if (cdiExtensions != null) {
SystemInstance.get().setComponent(LoaderService.class, new ExtensionAwareOptimizedLoaderService(cdiExtensions.value()));
}
// save the test under test to be able to retrieve it from extensions
// /!\ has to be done before all other init
SystemInstance.get().setComponent(TestInstance.class, new TestInstance(testClass, instance));
// call the mock injector before module method to be able to use mocked classes
// it will often use the TestInstance so
final Map<Object, List<Method>> mockInjectors = new HashMap<>();
findAnnotatedMethods(mockInjectors, MockInjector.class);
findAnnotatedMethods(mockInjectors, org.apache.openejb.junit.MockInjector.class);
if (!mockInjectors.isEmpty() && !mockInjectors.values().iterator().next().isEmpty()) {
final Map.Entry<Object, List<Method>> methods = mockInjectors.entrySet().iterator().next();
Object o = methods.getValue().iterator().next().invoke(methods.getKey());
if (o instanceof Class<?>) {
o = ((Class<?>) o).newInstance();
}
if (o instanceof FallbackPropertyInjector) {
SystemInstance.get().setComponent(FallbackPropertyInjector.class, (FallbackPropertyInjector) o);
}
}
for (final Map.Entry<Object, List<Method>> method : findAnnotatedMethods(new HashMap<>(), Component.class).entrySet()) {
for (final Method m : method.getValue()) {
setComponent(method.getKey(), m);
}
}
for (final Map.Entry<Object, List<Method>> method : findAnnotatedMethods(new HashMap<>(), org.apache.openejb.junit.Component.class).entrySet()) {
for (final Method m : method.getValue()) {
setComponent(method.getKey(), m);
}
}
final ConfigurationFactory config = new ConfigurationFactory();
config.init(SystemInstance.get().getProperties());
SystemInstance.get().setComponent(ConfigurationFactory.class, config);
assembler = new Assembler();
SystemInstance.get().setComponent(Assembler.class, assembler);
final OpenEjbConfiguration openEjbConfiguration;
if (openejb != null) {
openEjbConfiguration = config.getOpenEjbConfiguration(openejb);
} else {
openEjbConfiguration = config.getOpenEjbConfiguration();
}
assembler.buildContainerSystem(openEjbConfiguration);
if ("true".equals(configuration.getProperty(OpenEjbContainer.OPENEJB_EMBEDDED_REMOTABLE, "false")) || annotation != null || annotationOld != null) {
try {
if (annotation != null) {
final List<String> value = new ArrayList<>(asList(annotation.value()));
if (annotation.jaxrs()) {
value.add("jaxrs");
}
if (annotation.jaxws()) {
value.add("jaxws");
}
initFilteredServiceManager(value.toArray(new String[value.size()]));
}
if (annotationOld != null) {
initFilteredServiceManager(annotationOld.value());
}
serviceManager = new ServiceManagerProxy(false);
serviceManager.start();
} catch (final ServiceManagerProxy.AlreadyStartedException e) {
throw new OpenEJBRuntimeException(e);
}
}
if (propertiesToSetAgain != null) {
for (final String name : propertiesToSetAgain) {
final String value = PropertyPlaceHolderHelper.simpleValue(SystemInstance.get().getProperty(name));
configuration.put(name, value);
// done lazily to support placeholders so container will not do it here
JavaSecurityManagers.setSystemProperty(name, value);
}
propertiesToSetAgain.clear();
}
}
use of org.apache.openejb.assembler.classic.OpenEjbConfiguration in project tomee by apache.
the class WebServiceInjectionConfigurator method createServiceInfos.
private List<ServiceInfo> createServiceInfos(final Properties properties) {
final OpenEjbConfiguration config = SystemInstance.get().getComponent(OpenEjbConfiguration.class);
final List<ServiceInfo> services = new ArrayList<>(config.facilities != null && config.facilities.services != null ? config.facilities.services : Collections.<ServiceInfo>emptyList());
services.addAll(getServices(properties));
return services;
}
Aggregations