Search in sources :

Example 96 with Closeable

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;
}
Also used : AppModule(org.apache.openejb.config.AppModule) WebContext(org.apache.openejb.core.WebContext) SessionManager(org.apache.openejb.server.httpd.session.SessionManager) HttpSession(javax.servlet.http.HttpSession) MockHttpSession(org.apache.webbeans.web.lifecycle.test.MockHttpSession) Closeable(java.io.Closeable) AppContext(org.apache.openejb.AppContext) WebModule(org.apache.openejb.config.WebModule) IOException(java.io.IOException) LightweightWebAppBuilder(org.apache.openejb.web.LightweightWebAppBuilder) WebAppBuilder(org.apache.openejb.assembler.classic.WebAppBuilder) MockServletContext(org.apache.webbeans.web.lifecycle.test.MockServletContext) NamingException(javax.naming.NamingException) LifecycleException(org.jboss.arquillian.container.spi.client.container.LifecycleException) IOException(java.io.IOException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) DeploymentException(org.jboss.arquillian.container.spi.client.container.DeploymentException) WebAppInfo(org.apache.openejb.assembler.classic.WebAppInfo) AppInfo(org.apache.openejb.assembler.classic.AppInfo) LightweightWebAppBuilder(org.apache.openejb.web.LightweightWebAppBuilder) MockServletContext(org.apache.webbeans.web.lifecycle.test.MockServletContext) ServletContext(javax.servlet.ServletContext) MockHttpSession(org.apache.webbeans.web.lifecycle.test.MockHttpSession) DeploymentException(org.jboss.arquillian.container.spi.client.container.DeploymentException)

Example 97 with Closeable

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);
}
Also used : Connector(org.apache.catalina.connector.Connector) Container(org.apache.catalina.Container) Server(org.apache.catalina.Server) ServletContextListener(javax.servlet.ServletContextListener) Closeable(java.io.Closeable) Service(org.apache.catalina.Service) IOException(java.io.IOException) Engine(org.apache.catalina.Engine) ServletContextEvent(javax.servlet.ServletContextEvent)

Example 98 with Closeable

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);
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) InputStream(java.io.InputStream) Closeable(java.io.Closeable) XMLStreamReader(javax.xml.stream.XMLStreamReader) Reader(java.io.Reader) XMLFragmentStreamReader(org.apache.axiom.util.stax.XMLFragmentStreamReader) IOException(java.io.IOException) URL(java.net.URL) Detachable(org.apache.axiom.om.impl.common.builder.Detachable) XMLStreamException(javax.xml.stream.XMLStreamException) StAXPullInput(org.apache.axiom.om.impl.stream.stax.pull.StAXPullInput) OMException(org.apache.axiom.om.OMException)

Example 99 with Closeable

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);
    }
}
Also used : ApplicationSpecification(co.cask.cdap.api.app.ApplicationSpecification) CConfiguration(co.cask.cdap.common.conf.CConfiguration) Configuration(org.apache.hadoop.conf.Configuration) Closeable(java.io.Closeable) ArrayList(java.util.ArrayList) MapReduce(co.cask.cdap.api.mapreduce.MapReduce) NameMappedDatasetFramework(co.cask.cdap.internal.app.runtime.workflow.NameMappedDatasetFramework) DatasetFramework(co.cask.cdap.data2.dataset2.DatasetFramework) InstantiatorFactory(co.cask.cdap.common.lang.InstantiatorFactory) MetricsFieldSetter(co.cask.cdap.internal.app.runtime.MetricsFieldSetter) ProgramType(co.cask.cdap.proto.ProgramType) RunId(org.apache.twill.api.RunId) ProgramController(co.cask.cdap.app.runtime.ProgramController) MapReduceSpecification(co.cask.cdap.api.mapreduce.MapReduceSpecification) Arguments(co.cask.cdap.app.runtime.Arguments) MessagingService(co.cask.cdap.messaging.MessagingService) MetricsCollectionService(co.cask.cdap.api.metrics.MetricsCollectionService) Service(com.google.common.util.concurrent.Service) ProgramId(co.cask.cdap.proto.id.ProgramId) BasicProgramContext(co.cask.cdap.internal.app.runtime.BasicProgramContext) DataSetFieldSetter(co.cask.cdap.internal.app.runtime.DataSetFieldSetter) PropertyFieldSetter(co.cask.cdap.common.lang.PropertyFieldSetter) WorkflowProgramInfo(co.cask.cdap.internal.app.runtime.workflow.WorkflowProgramInfo) PluginInstantiator(co.cask.cdap.internal.app.runtime.plugin.PluginInstantiator) ProgramContextAware(co.cask.cdap.data.ProgramContextAware)

Example 100 with Closeable

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);
}
Also used : WorkflowActionNode(co.cask.cdap.api.workflow.WorkflowActionNode) Closeable(java.io.Closeable) ArrayList(java.util.ArrayList) ClassAcceptor(org.apache.twill.api.ClassAcceptor) ProgramId(co.cask.cdap.proto.id.ProgramId) WorkflowSpecification(co.cask.cdap.api.workflow.WorkflowSpecification) SchedulableProgramType(co.cask.cdap.api.schedule.SchedulableProgramType) ProgramType(co.cask.cdap.proto.ProgramType) SchedulableProgramType(co.cask.cdap.api.schedule.SchedulableProgramType) Resources(co.cask.cdap.api.Resources) ScheduleProgramInfo(co.cask.cdap.api.workflow.ScheduleProgramInfo) ProgramRunner(co.cask.cdap.app.runtime.ProgramRunner)

Aggregations

Closeable (java.io.Closeable)216 IOException (java.io.IOException)88 Test (org.junit.Test)56 ArrayList (java.util.ArrayList)29 File (java.io.File)26 HashMap (java.util.HashMap)12 VirtualFile (org.jboss.vfs.VirtualFile)12 URL (java.net.URL)9 Path (java.nio.file.Path)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)8 Map (java.util.Map)7 ISE (io.druid.java.util.common.ISE)6 InputStream (java.io.InputStream)6 MountHandle (org.jboss.as.server.deployment.module.MountHandle)6 ResourceRoot (org.jboss.as.server.deployment.module.ResourceRoot)6 ProgramController (co.cask.cdap.app.runtime.ProgramController)5 ProgramType (co.cask.cdap.proto.ProgramType)4 ProgramId (co.cask.cdap.proto.id.ProgramId)4 Pair (io.druid.java.util.common.Pair)4 FileOutputStream (java.io.FileOutputStream)4