public static void main()

in remoting/server/web/web.main/src/org/netbeans/modules/jackpot30/backend/main/WebMain.java [50:140]


    public static void main(String... origArgs) throws IOException, InterruptedException {
        int port = 9998;

        List<String> args = new ArrayList<String>(Arrays.asList(origArgs));

        if (args.size() > 1 && "--port".equals(args.get(0))) {
            args.remove(0);
            port = Integer.parseInt(args.remove(0));
        }

        if (args.size() != 1 && args.size() != 2) {
            System.err.println("Usage: java -jar " + WebMain.class.getProtectionDomain().getCodeSource().getLocation().getPath() + " [--port <port>] <cache> [<static-content>]");
            return ;
        }

        CategoryStorage.setCacheRoot(new File(args.get(0)));
        
//        org.netbeans.ProxyURLStreamHandlerFactory.register();
        URL.setURLStreamHandlerFactory(new RelStreamHandlerFactory());

        GrizzlyWebServer gws;

        if (args.size() == 2) {
            gws = new GrizzlyWebServer(port, args.get(1));
        } else {
            gws = new GrizzlyWebServer(port);
        }
        
        if (port == 0) {
            gws.getSelectorThread().setAddress(InetAddress.getByName("localhost"));
        }

        // Jersey web resources
        ServletAdapter jerseyAdapter = new ServletAdapter();
        jerseyAdapter.addInitParameter("com.sun.jersey.config.property.packages", "org.netbeans.modules.jackpot30");
        jerseyAdapter.addInitParameter("com.sun.jersey.spi.container.ContainerRequestFilters", AccessStatistics.class.getName());
//        jerseyAdapter.setContextPath("/");
        jerseyAdapter.setServletInstance(new ServletContainer());

        // register all above defined adapters
        gws.addGrizzlyAdapter(new GrizzlyAdapter(){
            public void service(GrizzlyRequest request, GrizzlyResponse response){
                if (request.getRequestURI().contains("/index/icons/")) {
                    String icon = request.getRequestURI().substring("/index".length());
                    URL iconURL = WebMain.class.getResource(icon);

                    if (iconURL == null) return;

                    InputStream in = null;
                    GrizzlyOutputStream out = null;

                    try {
                        in = iconURL.openStream();
                        out = response.createOutputStream();

                        int read;

                        while ((read = in.read()) != (-1)) {
                            out.write(read);
                        }
                    } catch (IOException ex) {
                        Exceptions.printStackTrace(ex);
                    } finally {
                        if (in != null) {
                            try {
                                in.close();
                            } catch (IOException ex) {
                                Exceptions.printStackTrace(ex);
                            }
                        }
                        if (out != null) {
                            try {
                                out.close();
                            } catch (IOException ex) {
                                Exceptions.printStackTrace(ex);
                            }
                        }
                    }
                }
                response.setStatus(404);
            }
        });
        gws.addGrizzlyAdapter(jerseyAdapter);

        // let Grizzly run
        gws.start();

        if (port == 0) {
            System.out.println("Running on port: " + gws.getSelectorThread().getPortLowLevel());
        }
    }