All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.fabric8.quickstarts.cxfcdi.ApplicationStarter Maven / Gradle / Ivy

There is a newer version: 2.2.28
Show newest version
/**
 *  Copyright 2005-2015 Red Hat, Inc.
 *
 *  Red Hat licenses this file to you under the Apache License, version
 *  2.0 (the "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 *  implied.  See the License for the specific language governing
 *  permissions and limitations under the License.
 */
package io.fabric8.quickstarts.cxfcdi;

import io.fabric8.cxf.endpoint.ManagedApi;
import io.fabric8.utils.Strings;
import io.fabric8.utils.Systems;

import org.apache.cxf.cdi.CXFCdiServlet;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.jboss.weld.environment.servlet.BeanManagerResourceBindingListener;
import org.jboss.weld.environment.servlet.Listener;

public class ApplicationStarter {
	
	public static void main(final String[] args) throws Exception {
		startServer().join();
	}
	
    public static Server startServer() throws Exception {

        // use system property first
        String port = System.getProperty("HTTP_PORT");
        if (port == null) {
            // and fallback to use environment variable
            port = System.getenv("HTTP_PORT");
        }
        if (port == null) {
            // and use port 8586 by default
            port = "8586";
        }
        Integer num = Integer.parseInt(port);
        String service = Systems.getEnvVarOrSystemProperty("WEB_CONTEXT_PATH", "WEB_CONTEXT_PATH", "quickstart-java-cxf-cdi");
        if (service == null) {
            // and fallback to use environment variable
            service = System.getenv("SERVICE");
        }
        if (service == null) {
            // and use 'quickstart-java-cxf-cdi' by default
            service = "quickstart-java-cxf-cdi";
        }
        String servicesPath = "/cxf/servicesList";

        String servletContextPath = "/" + service;
        ManagedApi.setSingletonCxfServletContext(servletContextPath);

        System.out.println("Starting REST server at:         http://localhost:" + port + servletContextPath + "/");
        System.out.println("View the services at:            http://localhost:" + port + servletContextPath + servicesPath);
        System.out.println("View an example REST service at: http://localhost:" + port + servletContextPath + "/cxfcdi/customerservice/customers/123");
        System.out.println();
        String url = "http://localhost:" + port + servletContextPath;
        if (!url.endsWith("/")) {
           url += "/";
        }

/*
        Map env = System.getenv();
        for (String envName : env.keySet()) {
            System.out.format("%s=%s%n", envName, env.get(envName));
        }
*/
        final Server server = new Server(num);

        // Register and map the dispatcher servlet
        final ServletHolder servletHolder = new ServletHolder(new CXFCdiServlet());
        
        // change default service list URI
        servletHolder.setInitParameter("service-list-path", servicesPath);

        final ServletContextHandler context = new ServletContextHandler();
        context.setContextPath("/");
        context.addEventListener(new Listener());
        context.addEventListener(new BeanManagerResourceBindingListener());
        
        String servletPath = "/*";
        if (Strings.isNotBlank(service)) {
         servletPath = servletContextPath + "/*";
        }
        context.addServlet(servletHolder, servletPath);
        
        server.setHandler(context);
        server.start();
        return server;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy