use of java.io.Closeable in project tomee by apache.
the class OpenEJBDeployableContainer method quickDeploy.
private DeploymentInfo quickDeploy(final Archive<?> archive, final TestClass testClass, final Closeables cls) throws DeploymentException {
final String name = archive.getName();
DeploymentInfo info = DEPLOYMENT_INFO.get(name);
if (info == null) {
try {
final AppModule module = OpenEJBArchiveProcessor.createModule(archive, testClass, cls);
final AppInfo appInfo = configurationFactory.configureApplication(module);
final WebAppBuilder webAppBuilder = SystemInstance.get().getComponent(WebAppBuilder.class);
final boolean isEmbeddedWebAppBuilder = webAppBuilder != null && LightweightWebAppBuilder.class.isInstance(webAppBuilder);
if (isEmbeddedWebAppBuilder) {
// for now we keep the same classloader, open to discussion if we should recreate it, not sure it does worth it
final LightweightWebAppBuilder lightweightWebAppBuilder = LightweightWebAppBuilder.class.cast(webAppBuilder);
for (final WebModule w : module.getWebModules()) {
final String moduleId = w.getModuleId();
lightweightWebAppBuilder.setClassLoader(moduleId, w.getClassLoader());
cls.add(new Closeable() {
@Override
public void close() throws IOException {
lightweightWebAppBuilder.removeClassLoader(moduleId);
}
});
}
}
final AppContext appCtx = assembler.createApplication(appInfo, module.getClassLoader());
if (isEmbeddedWebAppBuilder && PROPERTIES.containsKey(OpenEjbContainer.OPENEJB_EMBEDDED_REMOTABLE) && !appCtx.getWebContexts().isEmpty()) {
cls.add(new Closeable() {
@Override
public void close() throws IOException {
try {
final SessionManager sessionManager = SystemInstance.get().getComponent(SessionManager.class);
if (sessionManager != null) {
for (final WebContext web : appCtx.getWebContexts()) {
sessionManager.destroy(web);
}
}
} catch (final Throwable e) {
// no-op
}
}
});
}
final ServletContext appServletContext = new MockServletContext();
final HttpSession appSession = new MockHttpSession();
if (configuration.isStartDefaultScopes() && appCtx.getWebBeansContext() != null) {
startContexts(appCtx.getWebBeansContext().getContextsService(), appServletContext, appSession);
}
info = new DeploymentInfo(appServletContext, appSession, appInfo, appCtx);
if (configuration.isSingleDeploymentByArchiveName(name)) {
DEPLOYMENT_INFO.putIfAbsent(name, info);
}
} catch (final Exception e) {
throw new DeploymentException("can't deploy " + name, e);
}
}
return info;
}
use of java.io.Closeable in project tomee by apache.
the class LiveReloadInstaller method install.
public static void install(String path, final int port, final String folder) {
final Server server = TomcatHelper.getServer();
if (server == null) {
throw new IllegalStateException("tomcat not yet starting");
}
// checking which one is localhost could be better but should be fine
final Service service = server.findServices()[0];
final Engine engine = Engine.class.cast(service.getContainer());
final Container host = engine.findChild(engine.getDefaultHost());
if (LifecycleState.STARTED != host.getState()) {
throw new IllegalStateException("host not started, call LiveReloadInstaller.install() later.");
}
// add connector
final Connector connector = new Connector();
connector.setPort(port);
connector.setAttribute("connectionTimeout", "30000");
service.addConnector(connector);
// and the endpoint and start the watcher
final Closeable watch = Instances.get().getWatcher().watch(folder);
final LiveReloadWebapp liveReloadWebapp = new LiveReloadWebapp(path);
liveReloadWebapp.addApplicationLifecycleListener(new ServletContextListener() {
@Override
public void contextInitialized(final ServletContextEvent servletContextEvent) {
servletContextEvent.getServletContext().log("Started livereload server on port " + port);
}
@Override
public void contextDestroyed(final ServletContextEvent servletContextEvent) {
try {
watch.close();
} catch (final IOException e) {
// no-op: not that important, we shutdown anyway
}
}
});
host.addChild(liveReloadWebapp);
}
use of java.io.Closeable in project webservices-axiom by apache.
the class BuilderSpec method create.
private static BuilderSpec create(StAXParserConfiguration configuration, InputSource is, boolean makeDetachable) {
XMLStreamReader reader;
Detachable detachable;
Closeable closeable;
try {
if (is.getByteStream() != null) {
String systemId = is.getSystemId();
String encoding = is.getEncoding();
InputStream in = is.getByteStream();
if (makeDetachable) {
DetachableInputStream detachableInputStream = new DetachableInputStream(in, false);
in = detachableInputStream;
detachable = detachableInputStream;
} else {
detachable = null;
}
if (systemId != null) {
if (encoding == null) {
reader = StAXUtils.createXMLStreamReader(configuration, systemId, in);
} else {
throw new UnsupportedOperationException();
}
} else {
if (encoding == null) {
reader = StAXUtils.createXMLStreamReader(configuration, in);
} else {
reader = StAXUtils.createXMLStreamReader(configuration, in, encoding);
}
}
closeable = null;
} else if (is.getCharacterStream() != null) {
Reader in = is.getCharacterStream();
if (makeDetachable) {
DetachableReader detachableReader = new DetachableReader(in);
in = detachableReader;
detachable = detachableReader;
} else {
detachable = null;
}
reader = StAXUtils.createXMLStreamReader(configuration, in);
closeable = null;
} else {
String systemId = is.getSystemId();
InputStream in = new URL(systemId).openConnection().getInputStream();
if (makeDetachable) {
DetachableInputStream detachableInputStream = new DetachableInputStream(in, true);
in = detachableInputStream;
detachable = detachableInputStream;
} else {
detachable = null;
}
reader = StAXUtils.createXMLStreamReader(configuration, systemId, in);
closeable = in;
}
} catch (XMLStreamException ex) {
throw new OMException(ex);
} catch (IOException ex) {
throw new OMException(ex);
}
return new BuilderSpec(new StAXPullInput(reader, true, closeable), detachable);
}
use of java.io.Closeable in project cdap by caskdata.
the class MapReduceProgramRunner method run.
@Override
public ProgramController run(final Program program, ProgramOptions options) {
// Extract and verify parameters
ApplicationSpecification appSpec = program.getApplicationSpecification();
Preconditions.checkNotNull(appSpec, "Missing application specification.");
ProgramType processorType = program.getType();
Preconditions.checkNotNull(processorType, "Missing processor type.");
Preconditions.checkArgument(processorType == ProgramType.MAPREDUCE, "Only MAPREDUCE process type is supported.");
MapReduceSpecification spec = appSpec.getMapReduce().get(program.getName());
Preconditions.checkNotNull(spec, "Missing MapReduceSpecification for %s", program.getName());
Arguments arguments = options.getArguments();
RunId runId = ProgramRunners.getRunId(options);
WorkflowProgramInfo workflowInfo = WorkflowProgramInfo.create(arguments);
DatasetFramework programDatasetFramework = workflowInfo == null ? datasetFramework : NameMappedDatasetFramework.createFromWorkflowProgramInfo(datasetFramework, workflowInfo, appSpec);
// Setup dataset framework context, if required
if (programDatasetFramework instanceof ProgramContextAware) {
ProgramId programId = program.getId();
((ProgramContextAware) programDatasetFramework).setContext(new BasicProgramContext(programId.run(runId)));
}
MapReduce mapReduce;
try {
mapReduce = new InstantiatorFactory(false).get(TypeToken.of(program.<MapReduce>getMainClass())).create();
} catch (Exception e) {
LOG.error("Failed to instantiate MapReduce class for {}", spec.getClassName(), e);
throw Throwables.propagate(e);
}
// List of all Closeable resources that needs to be cleanup
List<Closeable> closeables = new ArrayList<>();
try {
PluginInstantiator pluginInstantiator = createPluginInstantiator(options, program.getClassLoader());
if (pluginInstantiator != null) {
closeables.add(pluginInstantiator);
}
final BasicMapReduceContext context = new BasicMapReduceContext(program, options, cConf, spec, workflowInfo, discoveryServiceClient, metricsCollectionService, txSystemClient, programDatasetFramework, streamAdmin, getPluginArchive(options), pluginInstantiator, secureStore, secureStoreManager, messagingService);
Reflections.visit(mapReduce, mapReduce.getClass(), new PropertyFieldSetter(context.getSpecification().getProperties()), new MetricsFieldSetter(context.getMetrics()), new DataSetFieldSetter(context));
// note: this sets logging context on the thread level
LoggingContextAccessor.setLoggingContext(context.getLoggingContext());
// Set the job queue to hConf if it is provided
Configuration hConf = new Configuration(this.hConf);
String schedulerQueue = options.getArguments().getOption(Constants.AppFabric.APP_SCHEDULER_QUEUE);
if (schedulerQueue != null && !schedulerQueue.isEmpty()) {
hConf.set(JobContext.QUEUE_NAME, schedulerQueue);
}
Service mapReduceRuntimeService = new MapReduceRuntimeService(injector, cConf, hConf, mapReduce, spec, context, program.getJarLocation(), locationFactory, streamAdmin, txSystemClient, authorizationEnforcer, authenticationContext);
mapReduceRuntimeService.addListener(createRuntimeServiceListener(program.getId(), runId, closeables, arguments, options.getUserArguments()), Threads.SAME_THREAD_EXECUTOR);
final ProgramController controller = new MapReduceProgramController(mapReduceRuntimeService, context);
LOG.debug("Starting MapReduce Job: {}", context);
// be running the job, but the data directory will be owned by cdap.
if (MapReduceTaskContextProvider.isLocal(hConf) || UserGroupInformation.isSecurityEnabled()) {
mapReduceRuntimeService.start();
} else {
ProgramRunners.startAsUser(cConf.get(Constants.CFG_HDFS_USER), mapReduceRuntimeService);
}
return controller;
} catch (Exception e) {
closeAllQuietly(closeables);
throw Throwables.propagate(e);
}
}
use of java.io.Closeable in project cdap by caskdata.
the class DistributedWorkflowProgramRunner method setupLaunchConfig.
@Override
protected void setupLaunchConfig(LaunchConfig launchConfig, Program program, ProgramOptions options, CConfiguration cConf, Configuration hConf, File tempDir) throws IOException {
WorkflowSpecification spec = program.getApplicationSpecification().getWorkflows().get(program.getName());
List<ClassAcceptor> acceptors = new ArrayList<>();
// Only interested in MapReduce and Spark nodes
Set<SchedulableProgramType> runnerTypes = EnumSet.of(SchedulableProgramType.MAPREDUCE, SchedulableProgramType.SPARK);
for (WorkflowActionNode node : Iterables.filter(spec.getNodeIdMap().values(), WorkflowActionNode.class)) {
// For each type, we only need one node to setup the launch context
ScheduleProgramInfo programInfo = node.getProgram();
if (!runnerTypes.remove(programInfo.getProgramType())) {
continue;
}
// Find the ProgramRunner of the given type and setup the launch context
ProgramType programType = ProgramType.valueOfSchedulableType(programInfo.getProgramType());
ProgramRunner runner = programRunnerFactory.create(programType);
try {
if (runner instanceof DistributedProgramRunner) {
// Call setupLaunchConfig with the corresponding program
ProgramId programId = program.getId().getParent().program(programType, programInfo.getProgramName());
((DistributedProgramRunner) runner).setupLaunchConfig(launchConfig, Programs.create(cConf, program, programId, runner), options, cConf, hConf, tempDir);
acceptors.add(launchConfig.getClassAcceptor());
}
} finally {
if (runner instanceof Closeable) {
Closeables.closeQuietly((Closeable) runner);
}
}
}
// Set the class acceptor
launchConfig.setClassAcceptor(new AndClassAcceptor(acceptors));
// Clear and set the runnable for the workflow driver
launchConfig.clearRunnables();
Resources resources = findDriverResources(program.getApplicationSpecification().getSpark(), program.getApplicationSpecification().getMapReduce(), spec);
resources = SystemArguments.getResources(options.getUserArguments(), resources);
launchConfig.addRunnable(spec.getName(), new WorkflowTwillRunnable(spec.getName()), resources, 1, 0);
}
Aggregations