Configuring Jetty parameter port using embedded Jetty

Hi,
I am using Embedded jetty to run my spring mvc application.
I am running it using
java -DFLOCK_APPS_CONFIG=staging/prod -jar snippet.jar

Open in new window

Now if the parameter is staging i need to run it on port 11001 and if its on prod i need to run in on 8080.
I am setting port like :
 private ServerConnector connector() {
        ServerConnector connector = new ServerConnector(server);
        connector.setPort(port);
        return connector;
    }

Open in new window


Here is my complete main java file :

package org.directi.code;

import org.apache.tomcat.InstanceManager;
import org.apache.tomcat.SimpleInstanceManager;
import org.eclipse.jetty.annotations.ServletContainerInitializersStarter;
import org.eclipse.jetty.apache.jsp.JettyJasperInitializer;
import org.eclipse.jetty.plus.annotation.ContainerInitializer;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.util.log.JavaUtilLog;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.webapp.WebAppContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;

public class AppStart {
    private static final Logger LOG = LoggerFactory.getLogger(AppStart.class.getName());
    private int port;
    private Server server;

    public static void main(String[] args) throws Exception {
        Log.setLog(new JavaUtilLog());
        LOG.info("robin suri");
        String env = System.getProperty("FLOCK_APPS_CONFIG");
        int port = Integer.parseInt(System.getProperty("PORT"));
        AppStart appStart = new AppStart(port);
        appStart.start();
        appStart.waitForInterrupt();
    }

    public AppStart(int port) {
        this.port = port;
    }

    public void start() throws Exception {
        server = new Server();
        ServerConnector connector = connector();
        server.addConnector(connector);
        WebAppContext webAppContext = getWebAppContext();
        server.setHandler(webAppContext);
        server.start();
    }

    private ServerConnector connector() {
        ServerConnector connector = new ServerConnector(server);
        connector.setPort(port);
        return connector;
    }

    private WebAppContext getWebAppContext() throws IOException {
        WebAppContext context = new WebAppContext();
        context.setContextPath("/");
        context.setDescriptor("webapp/WEB-INF/web.xml");
        context.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",
                ".*/[^/]*servlet-api-[^/]*\\.jar$|.*/javax.servlet.jsp.jstl-.*\\.jar$|.*/.*taglibs.*\\.jar$");
        context.setResourceBase(new ClassPathResource("webapp").getURI().toString());
        context.setAttribute("org.eclipse.jetty.containerInitializers", jspInitializers());
        context.setAttribute(InstanceManager.class.getName(), new SimpleInstanceManager());
        context.addBean(new ServletContainerInitializersStarter(context), true);
        context.setClassLoader(getUrlClassLoader());
        return context;
    }

    private List<ContainerInitializer> jspInitializers() {
        JettyJasperInitializer sci = new JettyJasperInitializer();
        ContainerInitializer initializer = new ContainerInitializer(sci, null);
        List<ContainerInitializer> initializers = new ArrayList<ContainerInitializer>();
        initializers.add(initializer);
        return initializers;
    }

    private ClassLoader getUrlClassLoader() {
        ClassLoader jspClassLoader = new URLClassLoader(new URL[0], this.getClass().getClassLoader());
        return jspClassLoader;
    }

    public void stop() throws Exception {
        server.stop();
    }

    public void waitForInterrupt() throws InterruptedException {
        server.join();
    }
}

Open in new window

And i am having folder structure liek : resources->properties->prod/staging
I want to put some jetty.properties file there which has port information corresponding to the environment
and then pick the proper port number.
What are ways i can do this ?
I think i will need to create a jetty.properties file having an entry like :
port=8080/11001
Then read that file and set the proper port number.
Please suggest if there are any better ways to do this ?
Thanks
Rohit BajajAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mccarlIT Business Systems Analyst / Software DeveloperCommented:
Something like this...

        String env = System.getProperty("FLOCK_APPS_CONFIG");
        Properties props = new Properties();
        props.load(AppStart.getClass().getResourceAsStream("/properties/" + env + "/jetty.properties"));
        int port = Integer.parseInt(props.getProperty("port"));
        AppStart appStart = new AppStart(port);

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.