Example usage for java.util List size

List of usage examples for java.util List size

Introduction

In this page you can find the example usage for java.util List size.

Prototype

int size();

Source Link

Document

Returns the number of elements in this list.

Usage

From source file:CourserankConnector.java

public static void main(String[] args) throws Exception {
    ///////////////////////////////////////
    //Tagger init

    //MaxentTagger tagger = new MaxentTagger("models/english-left3words-distsim.tagger");
    ////* ww  w  .ja  v  a2 s. com*/
    //CLIENT INITIALIZATION
    ImportData importCourse = new ImportData();
    HttpClient httpclient = new DefaultHttpClient();
    httpclient = WebClientDevWrapper.wrapClient(httpclient);
    try {
        /*
         httpclient.getCredentialsProvider().setCredentials(
            new AuthScope(null, -1),
            new UsernamePasswordCredentials("eadrian", "eactresp1"));
        */
        //////////////////////////////////////////////////
        //Get Course Bulletin Departments page
        List<Course> courses = new ArrayList<Course>();

        HttpGet httpget = new HttpGet("http://explorecourses.stanford.edu");

        System.out.println("executing request" + httpget.getRequestLine());
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();

        System.out.println("----------------------------------------");
        System.out.println(response.getStatusLine());
        String bulletinpage = "";

        //STORE RETURNED HTML TO BULLETINPAGE

        if (entity != null) {
            //System.out.println("Response content length: " + entity.getContentLength());
            InputStream i = entity.getContent();
            BufferedReader br = new BufferedReader(new InputStreamReader(i));
            String line;
            while ((line = br.readLine()) != null) {
                bulletinpage += line;
                //System.out.println(line);
            }
            br.close();
            i.close();
        }
        EntityUtils.consume(entity);

        ///////////////////////////////////////////////////////////////////////////////
        //Login to Courserank

        httpget = new HttpGet("https://courserank.com/stanford/main");

        System.out.println("executing request" + httpget.getRequestLine());
        response = httpclient.execute(httpget);
        entity = response.getEntity();

        System.out.println("----------------------------------------");
        System.out.println(response.getStatusLine());
        String page = "";
        if (entity != null) {
            System.out.println("Response content length: " + entity.getContentLength());
            InputStream i = entity.getContent();
            BufferedReader br = new BufferedReader(new InputStreamReader(i));
            String line;
            while ((line = br.readLine()) != null) {
                page += line;
                //System.out.println(line);
            }
            br.close();
            i.close();
        }
        EntityUtils.consume(entity);
        ////////////////////////////////////////////////////
        //POST REQUEST LOGIN

        HttpPost post = new HttpPost("https://www.courserank.com/stanford/main");

        List<NameValuePair> pairs = new ArrayList<NameValuePair>(2);

        pairs.add(new BasicNameValuePair("RT", ""));
        pairs.add(new BasicNameValuePair("action", "login"));
        pairs.add(new BasicNameValuePair("password", "trespass"));
        pairs.add(new BasicNameValuePair("username", "eaconte@stanford.edu"));
        post.setEntity(new UrlEncodedFormEntity(pairs));
        System.out.println("executing request" + post.getRequestLine());
        HttpResponse resp = httpclient.execute(post);
        HttpEntity ent = resp.getEntity();

        System.out.println("----------------------------------------");
        if (ent != null) {
            System.out.println("Response content length: " + ent.getContentLength());
            InputStream i = ent.getContent();
            BufferedReader br = new BufferedReader(new InputStreamReader(i));
            String line;
            while ((line = br.readLine()) != null) {
                //System.out.println(line);
            }
            br.close();
            i.close();
        }
        EntityUtils.consume(ent);
        ///////////////////////////////////////////////////
        //THIS STEP MAY NOT BE NEEDED BUT GETS MAIN PROFILE PAGE

        HttpGet gethome = new HttpGet("https://www.courserank.com/stanford/home");

        System.out.println("executing request" + gethome.getRequestLine());
        HttpResponse gresp = httpclient.execute(gethome);
        HttpEntity gent = gresp.getEntity();

        System.out.println("----------------------------------------");
        if (ent != null) {
            System.out.println("Response content length: " + gent.getContentLength());
            InputStream i = gent.getContent();
            BufferedReader br = new BufferedReader(new InputStreamReader(i));
            String line;
            while ((line = br.readLine()) != null) {
                //System.out.println(line);
            }
            br.close();
            i.close();
        }

        /////////////////////////////////////////////////////////////////////////////////
        //Parse Bulletin

        String results = getToken(bulletinpage, "RESULTS HEADER", "Additional Searches");
        String[] depts = results.split("href");

        //SPLIT FOR EACH DEPARTMENT LINK, ITERATE
        boolean ready = false;
        for (int i = 1; i < depts.length; i++) {
            //EXTRACT LINK, DEPARTMENT NAME AND ABBREVIATION
            String dept = new String(depts[i]);
            String abbr = getToken(dept, "(", ")");
            String name = getToken(dept, ">", "(");
            name.trim();
            //System.out.println(tagger.tagString(name));
            String link = getToken(dept, "=\"", "\">");
            System.out.println(name + " : " + abbr + " : " + link);

            System.out.println("======================================================================");

            if (i <= 10 || i >= 127) //values to keep it to undergraduate courses. Excludes law, med, business, overseas
                continue;
            /*if (i<=46)
               continue; */ //Start at BIOHOP
            /*if (abbr.equals("INTNLREL"))
               ready = true;
            if (!ready)
               continue;*/
            //Construct department course search URL
            //Then request page
            String URL = "http://explorecourses.stanford.edu/" + link
                    + "&filter-term-Autumn=on&filter-term-Winter=on&filter-term-Spring=on";
            httpget = new HttpGet(URL);

            //System.out.println("executing request" + httpget.getRequestLine());
            response = httpclient.execute(httpget);
            entity = response.getEntity();

            //ystem.out.println("----------------------------------------");
            //System.out.println(response.getStatusLine());
            String rpage = "";
            if (entity != null) {
                //System.out.println("Response content length: " + entity.getContentLength());
                InputStream in = entity.getContent();
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                String line;
                while ((line = br.readLine()) != null) {
                    rpage += line;
                    //System.out.println(line);
                }
                br.close();
                in.close();
            }
            EntityUtils.consume(entity);

            //Process results page
            List<Course> deptCourses = new ArrayList<Course>();
            List<Course> result = processResultPage(rpage);
            deptCourses.addAll(result);

            //While there are more result pages, keep going
            boolean more = (!(result.size() == 0) && (result.get((result.size() - 1)).courseNumber < 299));
            boolean morepages = anotherPage(rpage);
            while (morepages && more) {
                URL = nextURL(URL);
                httpget = new HttpGet(URL);

                //System.out.println("executing request" + httpget.getRequestLine());
                response = httpclient.execute(httpget);
                entity = response.getEntity();

                //System.out.println("----------------------------------------");
                //System.out.println(response.getStatusLine());
                rpage = "";
                if (entity != null) {
                    //System.out.println("Response content length: " + entity.getContentLength());
                    InputStream in = entity.getContent();
                    BufferedReader br = new BufferedReader(new InputStreamReader(in));
                    String line;
                    while ((line = br.readLine()) != null) {
                        rpage += line;
                        //System.out.println(line);
                    }
                    br.close();
                    in.close();
                }
                EntityUtils.consume(entity);
                morepages = anotherPage(rpage);
                result = processResultPage(rpage);
                deptCourses.addAll(result);
                more = (!(result.size() == 0) && (result.get((result.size() - 1)).courseNumber < 299));
                /*String mores = more? "yes": "no";
                String pagess = morepages?"yes":"no";
                System.out.println("more: "+mores+" morepages: "+pagess);
                System.out.println("more");*/
            }

            //Get course ratings for all department courses via courserank
            deptCourses = getRatings(httpclient, abbr, deptCourses);
            for (int j = 0; j < deptCourses.size(); j++) {
                Course c = deptCourses.get(j);
                System.out.println("" + c.title + " : " + c.rating);
                c.tags = name;
                c.code = c.code.trim();
                c.department = name;
                c.deptAB = abbr;
                c.writeToDatabase();
                //System.out.println(tagger.tagString(c.title));
            }

        }

        if (!page.equals(""))
            return;

        ///////////////////////////////////////////////////
        //Get Course Bulletin Department courses 

        /*
                
         httpget = new HttpGet("https://courserank.com/stanford/main");
                
         System.out.println("executing request" + httpget.getRequestLine());
         response = httpclient.execute(httpget);
         entity = response.getEntity();
                
         System.out.println("----------------------------------------");
         System.out.println(response.getStatusLine());
         page = "";
         if (entity != null) {
        System.out.println("Response content length: " + entity.getContentLength());
        InputStream i = entity.getContent();
        BufferedReader br = new BufferedReader(new InputStreamReader(i));
        String line;
        while ((line = br.readLine()) != null) {
           page +=line;
           //System.out.println(line);
        }
        br.close();
        i.close();
         }
         EntityUtils.consume(entity);
         ////////////////////////////////////////////////////
         //POST REQUEST LOGIN
                 
                 
         HttpPost post = new HttpPost("https://www.courserank.com/stanford/main");
                 
         List<NameValuePair> pairs = new ArrayList<NameValuePair>(2);
                 
                 
         pairs.add(new BasicNameValuePair("RT", ""));
         pairs.add(new BasicNameValuePair("action", "login"));
         pairs.add(new BasicNameValuePair("password", "trespass"));
         pairs.add(new BasicNameValuePair("username", "eaconte@stanford.edu"));
         post.setEntity(new UrlEncodedFormEntity(pairs));
         System.out.println("executing request" + post.getRequestLine());
         HttpResponse resp = httpclient.execute(post);
         HttpEntity ent = resp.getEntity();
                 
         System.out.println("----------------------------------------");
         if (ent != null) {
        System.out.println("Response content length: " + ent.getContentLength());
        InputStream i = ent.getContent();
        BufferedReader br = new BufferedReader(new InputStreamReader(i));
        String line;
        while ((line = br.readLine()) != null) {
           //System.out.println(line);
        }
        br.close();
        i.close();
         }
         EntityUtils.consume(ent);
         ///////////////////////////////////////////////////
         //THIS STEP MAY NOT BE NEEDED BUT GETS MAIN PROFILE PAGE
                 
         HttpGet gethome = new HttpGet("https://www.courserank.com/stanford/home");
                 
                 
         System.out.println("executing request" + gethome.getRequestLine());
         HttpResponse gresp = httpclient.execute(gethome);
         HttpEntity gent = gresp.getEntity();
                 
         System.out.println("----------------------------------------");
         if (ent != null) {
        System.out.println("Response content length: " + gent.getContentLength());
        InputStream i = gent.getContent();
        BufferedReader br = new BufferedReader(new InputStreamReader(i));
        String line;
        while ((line = br.readLine()) != null) {
           //System.out.println(line);
        }
        br.close();
        i.close();
         }
                 
                 
         ////////////////////////////////////////
         //GETS FIRST PAGE OF RESULTS
         EntityUtils.consume(gent);
                 
         post = new HttpPost("https://www.courserank.com/stanford/search_results");
                 
         pairs = new ArrayList<NameValuePair>(2);
                 
                 
         pairs.add(new BasicNameValuePair("filter_term_currentYear", "on"));
         pairs.add(new BasicNameValuePair("query", ""));
         post.setEntity(new UrlEncodedFormEntity(pairs));
         System.out.println("executing request" + post.getRequestLine());
         resp = httpclient.execute(post);
         ent = resp.getEntity();
                 
         System.out.println("----------------------------------------");
                 
         String rpage = "";
         if (ent != null) {
        System.out.println("Response content length: " + ent.getContentLength());
        InputStream i = ent.getContent();
        BufferedReader br = new BufferedReader(new InputStreamReader(i));
        String line;
        while ((line = br.readLine()) != null) {
           rpage += line;
           System.out.println(line);
        }
        br.close();
        i.close();
         }
         EntityUtils.consume(ent);
                 
         ////////////////////////////////////////////////////
         //PARSE FIRST PAGE OF RESULTS
                 
         //int index = rpage.indexOf("div class=\"searchItem");
         String []classSplit = rpage.split("div class=\"searchItem");
         for (int i=1; i<classSplit.length; i++) {
            String str = classSplit[i];
                    
            //ID
            String CID = getToken(str, "course?id=","\">");
                    
            // CODE 
            String CODE = getToken(str,"class=\"code\">" ,":</");
                    
            //TITLE 
            String NAME = getToken(str, "class=\"title\">","</");
                    
            //DESCRIP
            String DES = getToken(str, "class=\"description\">","</");
                    
            //TERM
            String TERM = getToken(str, "Terms:", "|");
                    
            //UNITS
            String UNITS = getToken(str, "Units:", "<br/>");
                
            //WORKLOAD
                    
            String WLOAD = getToken(str, "Workload:", "|");
                    
            //GER
            String GER = getToken(str, "GERs:", "</d");
                    
            //RATING
            int searchIndex = 0;
            float rating = 0;
            while (true) {
          int ratingIndex = str.indexOf("large_Full", searchIndex);
          if (ratingIndex ==-1) {
             int halfratingIndex = str.indexOf("large_Half", searchIndex);
             if (halfratingIndex == -1)
                break;
             else
                rating += .5;
             break;
          }
          searchIndex = ratingIndex+1;
          rating++;
                     
            }
            String RATING = ""+rating;
                    
            //GRADE
            String GRADE = getToken(str, "div class=\"unofficialGrade\">", "</");
            if (GRADE.equals("NOT FOUND")) {
          GRADE = getToken(str, "div class=\"officialGrade\">", "</");
            }
                    
            //REVIEWS
            String REVIEWS = getToken(str, "class=\"ratings\">", " ratings");
                    
                    
            System.out.println(""+CODE+" : "+NAME + " : "+CID);
            System.out.println("----------------------------------------");
            System.out.println("Term: "+TERM+" Units: "+UNITS+ " Workload: "+WLOAD + " Grade: "+ GRADE);
            System.out.println("Rating: "+RATING+ " Reviews: "+REVIEWS);
            System.out.println("==========================================");
            System.out.println(DES);
            System.out.println("==========================================");
                    
                    
                    
         }
                 
                 
         ///////////////////////////////////////////////////
         //GETS SECOND PAGE OF RESULTS
         post = new HttpPost("https://www.courserank.com/stanford/search_results");
                 
         pairs = new ArrayList<NameValuePair>(2);
                 
                 
         pairs.add(new BasicNameValuePair("filter_term_currentYear", "on"));
         pairs.add(new BasicNameValuePair("page", "2"));
         pairs.add(new BasicNameValuePair("query", ""));
         post.setEntity(new UrlEncodedFormEntity(pairs));
         System.out.println("executing request" + post.getRequestLine());
         resp = httpclient.execute(post);
         ent = resp.getEntity();
                 
         System.out.println("----------------------------------------");
         if (ent != null) {
        System.out.println("Response content length: " + ent.getContentLength());
        InputStream i = ent.getContent();
        BufferedReader br = new BufferedReader(new InputStreamReader(i));
        String line;
        while ((line = br.readLine()) != null) {
           //System.out.println(line);
        }
        br.close();
        i.close();
         }
         EntityUtils.consume(ent);
                 
         /*
         httpget = new HttpGet("https://github.com/");
                
         System.out.println("executing request" + httpget.getRequestLine());
         response = httpclient.execute(httpget);
         entity = response.getEntity();
                
         System.out.println("----------------------------------------");
         System.out.println(response.getStatusLine());
         page = "";
         if (entity != null) {
        System.out.println("Response content length: " + entity.getContentLength());
        InputStream i = entity.getContent();
        BufferedReader br = new BufferedReader(new InputStreamReader(i));
        String line;
        while ((line = br.readLine()) != null) {
           page +=line;
           System.out.println(line);
        }
        br.close();
        i.close();
         }*/
        EntityUtils.consume(entity);

    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpclient.getConnectionManager().shutdown();
    }
}

From source file:com.adobe.aem.demomachine.RegExp.java

public static void main(String[] args) throws IOException {

    String fileName = null;// w w w. jav  a 2 s  . c om
    String regExp = null;
    String position = null;
    String value = "n/a";
    List<String> allMatches = new ArrayList<String>();

    // Command line options for this tool
    Options options = new Options();
    options.addOption("f", true, "Filename");
    options.addOption("r", true, "RegExp");
    options.addOption("p", true, "Position");
    CommandLineParser parser = new BasicParser();
    try {
        CommandLine cmd = parser.parse(options, args);

        if (cmd.hasOption("f")) {
            fileName = cmd.getOptionValue("f");
        }

        if (cmd.hasOption("f")) {
            regExp = cmd.getOptionValue("r");
        }

        if (cmd.hasOption("p")) {
            position = cmd.getOptionValue("p");
        }

        if (fileName == null || regExp == null || position == null) {
            System.out.println("Command line parameters: -f fileName -r regExp -p position");
            System.exit(-1);
        }

    } catch (ParseException ex) {

        logger.error(ex.getMessage());

    }

    String content = readFile(fileName, Charset.defaultCharset());

    if (content != null) {
        Matcher m = Pattern.compile(regExp).matcher(content);
        while (m.find()) {
            String group = m.group();
            int pos = group.indexOf(".zip");
            if (pos > 0) {
                group = group.substring(0, pos);
            }
            logger.debug("RegExp: " + m.group() + " found returning " + group);
            allMatches.add(group);
        }

        if (allMatches.size() > 0) {

            if (position.equals("first")) {
                value = allMatches.get(0);
            }

            if (position.equals("last")) {
                value = allMatches.get(allMatches.size() - 1);
            }
        }
    }

    System.out.println(value);

}

From source file:com.gemini.geminimain.GeminiMain.java

public static void main(String[] args) throws IOException {
    //setup the mongodb access
    mongoClient = new MongoClient(DB_SERVER);

    //create table for the application, networks and servers
    ds = morphia.createDatastore(mongoClient, "Gemini");

    //create the mapper
    Injector injector = Guice.createInjector(new GeminiMapperModule());
    GeminiMapper mapper = injector.getInstance(GeminiMapper.class);

    //set the current logging level to debug
    Configurator.currentConfig().level(Level.INFO).activate();

    //create some data to transfer to the front end
    createSampleData();//www. j av a 2  s . c o  m

    //close the db client
    mongoClient.close();

    //check if authenticated, create the call context and user context here
    //for now it is empty!!!!
    before((request, response) -> {
        boolean authenticated = true;
        // ... check if authenticated
        if (!authenticated) {
            halt(401, "Nice try, you are not welcome here");
        }
    });

    after((request, response) -> {
        response.header("Access-Control-Allow-Origin", "*");
        //response.header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        //response.header("Access-Control-Max-Age", "3600");
        //response.header("Access-Control-Allow-Headers", "x-requested-with");
    });

    //get all environments of the tenant
    get("/environments/:tenantid", "application/json", (request, response) -> {
        String tenantID = request.params("tenantid");
        try {
            List<GeminiEnvironment> lEnvs = getEnvironments(tenantID);
            if (lEnvs != null) {
                response.status(200);
                Logger.info("Found environments for tenant {}", tenantID);
                List<GeminiEnvironmentDTO> dtoEnvs = new ArrayList();
                lEnvs.stream().forEach(e -> dtoEnvs.add(mapper.getDTOFromEnv(e)));
                return lEnvs;
            }
        } catch (UnknownHostException ex) {
            Logger.error("Severe Error: Unknown host - {}", DB_SERVER);
            response.status(500);
            return "Severe Error: Unknown database host " + DB_SERVER;
        }

        return "not implemented yet";
    }, new JsonTransformer());

    //return all applications for a given tenant and environment
    get("/applications/:tenantid/:envname", "application/json", (request, response) -> {
        String tenantID, envName;
        try {
            tenantID = URLDecoder.decode(request.params(":tenantid"), "UTF-8");
            envName = URLDecoder.decode(request.params(":envname"), "UTF-8");
        } catch (UnsupportedEncodingException ex) {
            Logger.error("Severe Error: Unsupported encoding in URL - server Name: {} Exception: {}",
                    request.params(":name"), ex);
            return "Severe Error: Unsupported encoding in URL";
        }

        try {
            List<GeminiApplication> apps = getEnvApplications(tenantID, envName);
            if (apps == null || apps.isEmpty()) {
                response.status(404);
                Logger.info("Could not find any applications.");
                return "No Applications found.";
            } else {
                response.status(200);
                Logger.debug("Found applications");
                List<GeminiApplicationDTO> dtoApps = new ArrayList();
                apps.stream().forEach((a) -> {
                    dtoApps.add(mapper.getDTOFromApp(a));
                });
                return dtoApps;
            }
        } catch (UnknownHostException ex) {
            Logger.error("Severe Error: Unknown host - {}", DB_SERVER);
            response.status(500);
            return "Severe Error: Unknown database host " + DB_SERVER;
        }
    }, new JsonTransformer());

    //return application given a name
    get("/applications/:name", "application/json", (request, response) -> {
        String appName = "";
        //decode the URL as it may contain escape characters, etc.
        try {
            appName = URLDecoder.decode(request.params(":name"), "UTF-8");
        } catch (UnsupportedEncodingException ex) {
            Logger.error("Severe Error: Unsupported encoding in URL - application Name: {} Exception: {}",
                    request.params(":name"), ex);
            return "Severe Error: Unsupported encoding in URL - server name " + appName;
        }
        try {
            GeminiApplication a = getAppByName(appName);
            if (a != null) {
                Logger.debug("Found application {}", appName);
                return mapper.getDTOFromApp(a);
            } else {
                Logger.info("Could not find application {}", appName);
                return "Could not find application " + appName;
            }
        } catch (UnknownHostException ex) {
            Logger.error("Severe Error: Unknown host - {} Exception: {}", DB_SERVER, ex);
            response.status(500);
            return "Severe Error: Unknown database host " + DB_SERVER;
        }
    }, new JsonTransformer());

    post("/applications", (request, response) -> {
        String body = request.body();
        return "Hello World: " + request.body();
    });

    //return all networks related to application with ID = ':id'
    get("/applications/:name/networks", "application/json", (Request request, Response response) -> {
        String appName;
        //decode the URL as it may contain escape characters, etc.
        try {
            appName = URLDecoder.decode(request.params(":name"), "UTF-8");
        } catch (UnsupportedEncodingException ex) {
            Logger.error("Severe Error: Unsupported encoding in URL - server Name: {} Exception: {}",
                    request.params(":name"), ex);
            return "Severe Error: Unsupported encoding in URL";
        }
        try {
            List<GeminiNetwork> lNet = getAppNetworks(appName);
            if (lNet != null) {
                Logger.debug("Found networks for application {}", appName);
                List<GeminiNetworkDTO> dtoNets = new ArrayList();
                lNet.stream().forEach(aNet -> dtoNets.add(mapper.getDTOFromNetwork(aNet)));
                return dtoNets;
            } else {
                response.status(404);
                Logger.info("Could not find any networks for application {}", appName);
                return "Could not find any networks for application: " + appName;
            }
        } catch (UnknownHostException ex) {
            Logger.error("Severe Error: Unknown host - {} Exception: {}", DB_SERVER, ex);
            response.status(500);
            return "Severe Error: Unknown database host " + DB_SERVER;
        }
    }, new JsonTransformer());

    //return all servers related to application with ID = ':id'
    get("/applications/:id/servers", "application/json", (Request request, Response response) -> {
        String appName = "";
        //decode the URL as it may contain escape characters, etc.
        try {
            appName = URLDecoder.decode(request.params(":name"), "UTF-8");
        } catch (UnsupportedEncodingException ex) {
            Logger.error("Severe Error: Unsupported encoding in URL - server Name: {} Exception: {}",
                    request.params(":name"), ex);
            return "Severe Error: Unsupported encoding in URL";
        }
        try {
            List<GeminiServer> lSrv = getAppServers(appName);
            if (lSrv != null) {
                Logger.debug("Found servers for application {}", appName);
                List<GeminiServerDTO> dtoSrvs = new ArrayList();
                for (GeminiServer s : lSrv) {
                    dtoSrvs.add(mapper.getDTOFromServer(s));
                }
                return dtoSrvs;
            } else {
                Logger.info("Could not find servers for application {}", appName);
                response.status(404);
                return "Could not find servers for application: " + appName;
            }
        } catch (UnknownHostException ex) {
            Logger.error("Severe Error: Unknown host - {} Exception: {}", DB_SERVER, ex);
            return "Severe Error: Unknown database host " + DB_SERVER;
        }
    }, new JsonTransformer());

    //return all servers related to application with ID = ':appID' AND network with ID = ':nID'
    get("/applications/:appname/networks/:netstart/:netend/servers", "application/json",
            (request, response) -> {
                String appName = "", netStart = "", netEnd = "";
                //decode the URL as it may contain escape characters, etc.
                try {
                    appName = URLDecoder.decode(request.params(":appname"), "UTF-8");
                    netStart = URLDecoder.decode(request.params(":netstart"), "UTF-8");
                    netEnd = URLDecoder.decode(request.params(":netend"), "UTF-8");
                } catch (UnsupportedEncodingException ex) {
                    Logger.error(
                            "Severe Error: Unsupported encoding in URL - application {} with network start: {} and end: {} Exception {}",
                            request.params(":appname"), request.params(":netstart"), request.params(":netend"),
                            ex);
                    return "Severe Error: Unsupported encoding in URL";
                }

                //get the servers for app network
                try {
                    List<GeminiServer> lSrv = getAppNetworkServers(appName, netStart, netEnd);
                    if (lSrv == null || lSrv.isEmpty()) {
                        Logger.info("No servers for application {} with network start: {} and end: {}", appName,
                                netStart, netEnd);
                        response.status(404);
                        return "No servers for application " + appName + " with network start: " + netStart
                                + " and end: " + netEnd;
                    } else {
                        Logger.debug("Found servers for application {} with network start: {} and end: ",
                                appName, netStart, netEnd);
                        List<GeminiServerDTO> dtoSrvs = new ArrayList();
                        for (GeminiServer s : lSrv) {
                            dtoSrvs.add(mapper.getDTOFromServer(s));
                        }
                        return dtoSrvs;
                    }
                } catch (UnknownHostException ex) {
                    Logger.error("Severe Error: Unknown host - {} Exception: {}", DB_SERVER, ex);
                    return "Severe Error: Unknown database host " + DB_SERVER;
                }
            }, new JsonTransformer());

    //get the networks for a tenant and environment,
    get("/networks/:tenantid/:envname", "application/json", (request, response) -> {
        String tenantID, envName;
        try {
            tenantID = URLDecoder.decode(request.params(":tenantid"), "UTF-8");
            envName = URLDecoder.decode(request.params(":envname"), "UTF-8");
        } catch (UnsupportedEncodingException ex) {
            Logger.error("Severe Error: Unsupported encoding in URL - server Name: {} Exception: {}",
                    request.params(":name"), ex);
            return "Severe Error: Unsupported encoding in URL";
        }
        try {
            List<GeminiNetwork> nets = getEnvNetworks(tenantID, envName);
            if (nets == null) {
                Logger.info("No networks discovered for tenant {} in environment {}", tenantID, envName);
                return "No networks discovered for tenant" + tenantID + "in environment" + envName;
            } else {
                response.status(200);
                List<GeminiNetworkDTO> dtoNets = new ArrayList();
                nets.stream().forEach(n -> dtoNets.add(mapper.getDTOFromNetwork(n)));
                Logger.debug("Found {} networks for tenant {} env {}", nets.size(), tenantID, envName);
                return dtoNets;
            }
        } catch (UnknownHostException ex) {
            Logger.error("Severe Error: Unknown datbase host - {}", DB_SERVER);
            return "Severe Error: Unknown data host " + DB_SERVER;
        }
    }, new JsonTransformer());

    get("/networks/:netstart/:netend", "application/json", (request, response) -> {
        String netStart = "", netEnd = "";
        //decode the URL as it may contain escape characters, etc.
        try {
            netStart = URLDecoder.decode(request.params(":netstart"), "UTF-8");
            netEnd = URLDecoder.decode(request.params(":netend"), "UTF-8");
        } catch (UnsupportedEncodingException ex) {
            Logger.error("Severe Error: Unsupported encoding in URL - netStart: {} netEnd: {} Exception: {}",
                    request.params(":netstart"), request.params(":netend"), ex);
            return "Severe Error: Unsupported encoding in URL";
        }
        try {
            GeminiNetwork n = getNetworkFromDB(netStart, netEnd);
            if (n == null) {
                Logger.info("No network with start {} and end {} found", netStart, netEnd);
                return "No network with start " + netStart + " and end " + netEnd + " found";
            } else {
                Logger.debug("Found network with start {} and end {} ", netStart, netEnd);
                return mapper.getDTOFromNetwork(n);
            }
        } catch (UnknownHostException ex) {
            Logger.error("Severe Error: Unknown host - {} Exception: {}", DB_SERVER, ex);
            return "Severe Error: Unknown database host " + DB_SERVER;
        }
    }, new JsonTransformer());

    get("/networks/:netstart/:netend/servers", "application/json", (request, response) -> {
        String netStart = "", netEnd = "";
        //decode the URL as it may contain escape characters, etc.
        try {
            netStart = URLDecoder.decode(request.params(":netstart"), "UTF-8");
            netEnd = URLDecoder.decode(request.params(":netend"), "UTF-8");
        } catch (UnsupportedEncodingException ex) {
            Logger.error("Severe Error: Unsupported encoding in URL - netStart: {} netEnd: {} Exception: {}",
                    request.params(":netstart"), request.params(":netend"), ex);
            return "Severe Error: Unsupported encoding in URL";
        }
        try {
            List<GeminiServer> lSrv = getNetworkServersFromDB(netStart, netEnd);
            if (lSrv == null) {
                Logger.info("No servers in network with start {} and end {} found", netStart, netEnd);
                return "No servers in network with start " + netStart + " and end " + netEnd + " found";
            } else {
                Logger.debug("Found servers in network with start {} and end {} ", netStart, netEnd);
                List<GeminiServerDTO> dtoSrvs = new ArrayList();
                for (GeminiServer s : lSrv) {
                    dtoSrvs.add(mapper.getDTOFromServer(s));
                }
                return dtoSrvs;
            }
        } catch (UnknownHostException ex) {
            Logger.error("Severe Error: Unknown host - {} Exception: {}", DB_SERVER, ex);
            return "Severe Error: Unknown database host " + DB_SERVER;
        }
    }, new JsonTransformer());

    post("/networks/:netstart/:netend", "application/json", (request, response) -> {
        String netStart = "", netEnd = "";
        //decode the URL as it may contain escape characters, etc.
        try {
            netStart = URLDecoder.decode(request.params(":netstart"), "UTF-8");
            netEnd = URLDecoder.decode(request.params(":netend"), "UTF-8");
        } catch (UnsupportedEncodingException ex) {
            Logger.error("Severe Error: Unsupported encoding in URL - netStart: {} netEnd: {} Exception: {}",
                    request.params(":netstart"), request.params(":netend"), ex);
            return "Severe Error: Unsupported encoding in URL";
        }

        //return the discovered networks
        //            DiscoverNetworkRange newNet = new DiscoverNetworkRange(netStart, netEnd);
        //            List<GeminiNetwork> lNet;
        //            if (autoDiscover) {
        //                try {
        //                    //start discovering...
        //                    lNet = discoverNetworks(netStart, netEnd);
        //                } catch (IOException ex) {
        //                    java.util.logging.Logger.getLogger(GeminiMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        //                }
        //            }

        //since all the services are running on the same computer
        response.header("Access-Control-Allow-Origin", "*");

        //return the networks...
        return "no networks";
    }, new JsonTransformer());

    //get all servers for tenant within an environment
    get("/servers", "application/json", (request, response) -> {
        try {
            List<GeminiServer> srvs = getServersFromDB();
            if (srvs == null) {
                Logger.info("Found no servers in database");
                return "No Networks";
            } else {
                Logger.debug("Found servers in database");
                List<GeminiServerDTO> dtoSrvs = new ArrayList();
                for (GeminiServer s : srvs) {
                    dtoSrvs.add(mapper.getDTOFromServer(s));
                }
                response.status(200);
                return dtoSrvs;
            }
        } catch (UnknownHostException ex) {
            Logger.error("Severe Error: Unknown host - {} Exception: {}", DB_SERVER, ex);
            return "Severe Error: Unknown database host " + DB_SERVER;
        }
    }, new JsonTransformer());

    get("/servers/:name", "application/json", (request, response) -> {
        String srvName;
        try {
            srvName = URLDecoder.decode(request.params(":name"), "UTF-8");
        } catch (UnsupportedEncodingException ex) {
            Logger.error("Severe Error: Unsupported encoding in URL - {} Exception {}", request.params(":name"),
                    ex);
            return "Severe Error: Unsupported encoding in URL";
        }
        try {
            GeminiServer s = getServerFromDB(srvName);
            if (s == null) {
                Logger.info("No server with name {} found", srvName);
                return "No server with name " + srvName;

            } else {
                Logger.debug("Found server with name {}", srvName);
                return mapper.getDTOFromServer(s);
            }

        } catch (UnknownHostException ex) {
            Logger.error("Severe Error: Unknown host - {} Exception: {}", DB_SERVER, ex);
            return "Severe Error: Unknown database host " + DB_SERVER;
        }
    }, new JsonTransformer());

}

From source file:com.github.brandtg.stl.StlDecomposition.java

/**
 * Runs STL on a CSV of time,measure.//from w  w w  .ja  v a 2s.  c om
 *
 * <p>
 *   Outputs a CSV of time,measure,trend,seasonal,remainder.
 * </p>
 *
 * @param args
 *  args[0] = numberOfObservations
 * @throws Exception
 *  If could not process data
 */
public static void main(String[] args) throws Exception {
    List<Number> times = new ArrayList<Number>();
    List<Number> measures = new ArrayList<Number>();

    // Read from STDIN
    String line;
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    while ((line = reader.readLine()) != null) {
        String[] tokens = line.split(",");
        times.add(Long.valueOf(tokens[0]));
        measures.add(Double.valueOf(tokens[1]));
    }

    // Compute STL
    StlDecomposition stl = new StlDecomposition(Integer.valueOf(args[0]));
    stl.getConfig().setSeasonalComponentBandwidth(Double.valueOf(
            System.getProperty("seasonal.bandwidth", String.valueOf(StlConfig.DEFAULT_SEASONAL_BANDWIDTH))));
    stl.getConfig().setTrendComponentBandwidth(Double
            .valueOf(System.getProperty("trend.bandwidth", String.valueOf(StlConfig.DEFAULT_TREND_BANDWIDTH))));
    stl.getConfig().setNumberOfInnerLoopPasses(Integer
            .valueOf(System.getProperty("inner.loop", String.valueOf(StlConfig.DEFAULT_INNER_LOOP_PASSES))));
    StlResult res = stl.decompose(times, measures);

    // Output to STDOUT
    for (int i = 0; i < times.size(); i++) {
        System.out.println(String.format("%d,%02f,%02f,%02f,%02f", (long) res.getTimes()[i], res.getSeries()[i],
                res.getTrend()[i], res.getSeasonal()[i], res.getRemainder()[i]));
    }
}

From source file:de.huxhorn.lilith.Lilith.java

public static void main(String[] args) {
    {//from   ww  w  .  j a  va  2s  . c  o  m
        // initialize java.util.logging to use slf4j...
        Handler handler = new Slf4JHandler();
        java.util.logging.Logger rootLogger = java.util.logging.Logger.getLogger("");
        rootLogger.addHandler(handler);
        rootLogger.setLevel(java.util.logging.Level.WARNING);
    }

    StringBuilder appTitle = new StringBuilder();
    appTitle.append(APP_NAME).append(" V").append(APP_VERSION);
    if (APP_SNAPSHOT) {
        // always append timestamp for SNAPSHOT
        appTitle.append(" (").append(APP_TIMESTAMP_DATE).append(")");
    }

    CommandLineArgs cl = new CommandLineArgs();
    JCommander commander = new JCommander(cl);
    Cat cat = new Cat();
    commander.addCommand(Cat.NAME, cat);
    Tail tail = new Tail();
    commander.addCommand(Tail.NAME, tail);
    Filter filter = new Filter();
    commander.addCommand(Filter.NAME, filter);
    Index index = new Index();
    commander.addCommand(Index.NAME, index);
    Md5 md5 = new Md5();
    commander.addCommand(Md5.NAME, md5);
    Help help = new Help();
    commander.addCommand(Help.NAME, help);

    try {
        commander.parse(args);
    } catch (ParameterException ex) {
        printAppInfo(appTitle.toString(), false);
        System.out.println(ex.getMessage() + "\n");
        printHelp(commander);
        System.exit(-1);
    }
    if (cl.verbose) {
        if (!APP_SNAPSHOT) {
            // timestamp is always appended for SNAPSHOT
            // don't append it twice
            appTitle.append(" (").append(APP_TIMESTAMP_DATE).append(")");
        }
        appTitle.append(" - ").append(APP_REVISION);
    }

    String appTitleString = appTitle.toString();
    if (cl.showHelp) {
        printAppInfo(appTitleString, false);
        printHelp(commander);
        System.exit(0);
    }

    String command = commander.getParsedCommand();
    if (!Tail.NAME.equals(command) && !Cat.NAME.equals(command) && !Filter.NAME.equals(command)) // don't print info in case of cat, tail or filter
    {
        printAppInfo(appTitleString, true);
    }

    if (cl.logbackConfig != null) {
        File logbackFile = new File(cl.logbackConfig);
        if (!logbackFile.isFile()) {
            System.out.println(logbackFile.getAbsolutePath() + " is not a valid file.");
            System.exit(-1);
        }
        try {
            initLogbackConfig(logbackFile.toURI().toURL());
        } catch (MalformedURLException e) {
            System.out.println("Failed to convert " + logbackFile.getAbsolutePath() + " to URL. " + e);
            System.exit(-1);
        }
    } else if (cl.verbose) {
        initVerboseLogging();
    }

    if (cl.printBuildTimestamp) {
        System.out.println("Build-Date     : " + APP_TIMESTAMP_DATE);
        System.out.println("Build-Revision : " + APP_REVISION);
        System.out.println("Build-Timestamp: " + APP_TIMESTAMP);
        System.exit(0);
    }

    if (Help.NAME.equals(command)) {
        commander.usage();
        if (help.commands == null || help.commands.size() == 0) {
            commander.usage(Help.NAME);
        } else {
            Map<String, JCommander> commands = commander.getCommands();
            for (String current : help.commands) {
                if (commands.containsKey(current)) {
                    commander.usage(current);
                } else {
                    System.out.println("Unknown command '" + current + "'!");
                }
            }
        }
        System.exit(0);
    }

    if (Md5.NAME.equals(command)) {
        List<String> files = md5.files;
        if (files == null || files.isEmpty()) {
            printHelp(commander);
            System.out.println("Missing files!");
            System.exit(-1);
        }
        boolean error = false;
        for (String current : files) {
            if (!CreateMd5Command.createMd5(new File(current))) {
                error = true;
            }
        }
        if (error) {
            System.exit(-1);
        }
        System.exit(0);
    }

    if (Index.NAME.equals(command)) {
        if (!cl.verbose && cl.logbackConfig == null) {
            initCLILogging();
        }
        List<String> files = index.files;
        if (files == null || files.size() == 0) {
            printHelp(commander);
            System.exit(-1);
        }
        boolean error = false;
        for (String current : files) {
            if (!IndexCommand.indexLogFile(new File(current))) {
                error = true;
            }
        }
        if (error) {
            System.exit(-1);
        }
        System.exit(0);
    }

    if (Cat.NAME.equals(command)) {
        if (!cl.verbose && cl.logbackConfig == null) {
            initCLILogging();
        }
        List<String> files = cat.files;
        if (files == null || files.size() != 1) {
            printHelp(commander);
            System.exit(-1);
        }
        if (CatCommand.catFile(new File(files.get(0)), cat.pattern, cat.numberOfLines)) {
            System.exit(0);
        }
        System.exit(-1);
    }

    if (Tail.NAME.equals(command)) {
        if (!cl.verbose && cl.logbackConfig == null) {
            initCLILogging();
        }
        List<String> files = tail.files;
        if (files == null || files.size() != 1) {
            printHelp(commander);
            System.exit(-1);
        }
        if (TailCommand.tailFile(new File(files.get(0)), tail.pattern, tail.numberOfLines, tail.keepRunning)) {
            System.exit(0);
        }
        System.exit(-1);
    }

    if (Filter.NAME.equals(command)) {
        if (!cl.verbose && cl.logbackConfig == null) {
            initCLILogging();
        }
        if (FilterCommand.filterFile(new File(filter.input), new File(filter.output),
                new File(filter.condition), filter.searchString, filter.pattern, filter.overwrite,
                filter.keepRunning, filter.exclusive)) {
            System.exit(0);
        }
        System.exit(-1);
    }

    if (cl.flushPreferences) {
        flushPreferences();
    }

    if (cl.exportPreferencesFile != null) {
        exportPreferences(cl.exportPreferencesFile);
    }

    if (cl.importPreferencesFile != null) {
        importPreferences(cl.importPreferencesFile);
    }

    if (cl.exportPreferencesFile != null || cl.importPreferencesFile != null) {
        System.exit(0);
    }

    if (cl.flushLicensed) {
        flushLicensed();
    }

    startLilith(appTitleString);
}

From source file:edu.ucsd.crbs.cws.App.java

License:asdf

public static void main(String[] args) {
    Job.REFS_ENABLED = false;//from ww  w. j  a  v a  2 s  . c  o  m
    Workflow.REFS_ENABLED = false;
    try {

        OptionParser parser = new OptionParser() {
            {
                accepts(UPLOAD_WF_ARG, "Add/Update Workflow").withRequiredArg().ofType(File.class)
                        .describedAs("Kepler .kar file");
                //accepts(LOAD_TEST,"creates lots of workflows and jobs");
                accepts(SYNC_WITH_CLUSTER_ARG,
                        "Submits & Synchronizes Workflow Jobs on local cluster with CRBS Workflow Webservice.  Requires --"
                                + PROJECT_ARG + " --" + PORTALNAME_ARG + " --" + PORTAL_URL_ARG + " --"
                                + HELP_EMAIL_ARG).withRequiredArg().ofType(String.class).describedAs("URL");
                accepts(GEN_OLD_KEPLER_XML_ARG, "Generates version 1.x kepler xml for given workflow")
                        .withRequiredArg().ofType(String.class).describedAs("wfid or .kar file");
                accepts(UPLOAD_FILE_ARG, "Registers and uploads Workspace file to REST service")
                        .withRequiredArg().ofType(File.class);
                accepts(REGISTER_FILE_ARG,
                        "Registers Workspace file to REST service (DOES NOT UPLOAD FILE TO REST SERVICE)")
                                .withRequiredArg().ofType(File.class);
                accepts(GET_WORKSPACE_FILE_INFO_ARG, "Outputs JSON of specified workspace file(s)")
                        .withRequiredArg().ofType(String.class).describedAs("workspace file id");
                accepts(GET_WORKFLOW_ARG, "Outputs JSON of specified Workflow").withRequiredArg()
                        .ofType(Long.class).describedAs("Workflow Id");
                accepts(DOWNLOAD_FILE_ARG, "Downloads Workspace file").withRequiredArg().ofType(String.class)
                        .describedAs("workspace file id");
                accepts(UPDATE_PATH_ARG, "Updates Workspace file path").withRequiredArg().ofType(String.class)
                        .describedAs("workspace file id");
                accepts(PATH_ARG,
                        "Sets WorkspaceFile file path.  Used in coordination with --" + UPDATE_PATH_ARG)
                                .withRequiredArg().ofType(String.class).describedAs("file path");
                accepts(URL_ARG,
                        "URL to use with --" + UPLOAD_WF_ARG + ", --" + UPLOAD_FILE_ARG + ", --"
                                + GET_WORKSPACE_FILE_INFO_ARG + " flags").withRequiredArg().ofType(String.class)
                                        .describedAs("URL");
                accepts(EXAMPLE_JSON_ARG,
                        "Outputs example JSON of Job, User, Workflow, and WorkspaceFile objects");
                accepts(WF_EXEC_DIR_ARG, "Workflow Execution Directory").withRequiredArg().ofType(File.class)
                        .describedAs("Directory");
                accepts(WF_DIR_ARG, "Workflows Directory").withRequiredArg().ofType(File.class)
                        .describedAs("Directory");
                accepts(KEPLER_SCRIPT_ARG, "Kepler").withRequiredArg().ofType(File.class).describedAs("Script");
                accepts(QUEUE_ARG, "SGE Queue").withRequiredArg().ofType(String.class).describedAs("Queue");
                accepts(CAST_ARG, "Panfishcast binary").withRequiredArg().ofType(File.class)
                        .describedAs("panfishcast");
                accepts(STAT_ARG, "Panfishstat binary").withRequiredArg().ofType(File.class)
                        .describedAs("panfishstat");
                accepts(LOGIN_ARG, "User Login").withRequiredArg().ofType(String.class).describedAs("username");
                accepts(TOKEN_ARG, "User Token").withRequiredArg().ofType(String.class).describedAs("token");
                accepts(RUN_AS_ARG, "User to run as (for power accounts that can run as other users)")
                        .withRequiredArg().ofType(String.class).describedAs("runas");
                accepts(OWNER_ARG, "Sets owner when creating Workspace file and Workflow").withRequiredArg()
                        .ofType(String.class).describedAs("username");
                accepts(JOB_ID_ARG, "Sets source job id for Workspace file when used with --" + UPLOAD_FILE_ARG
                        + " and --" + REGISTER_FILE_ARG).withRequiredArg().ofType(Long.class)
                                .describedAs("Job Id");
                accepts(MD5_ARG,
                        "Sets md5 for Workspace file when used with --" + UPLOAD_FILE_ARG + " and --"
                                + REGISTER_FILE_ARG).withRequiredArg().ofType(String.class)
                                        .describedAs("MD5 message digest");
                accepts(SIZE_ARG,
                        "Sets size in bytes for Workspace file when used with --" + UPLOAD_FILE_ARG + " and --"
                                + REGISTER_FILE_ARG).withRequiredArg().ofType(Long.class)
                                        .describedAs("Size of file/dir in bytes");
                accepts(RESAVE_WORKSPACEFILE_ARG, "Resaves Workspace file").withRequiredArg().ofType(Long.class)
                        .describedAs("WorkspaceFile Id or -1 to resave all");
                accepts(RESAVE_JOB_ARG, "Resaves Job").withRequiredArg().ofType(Long.class)
                        .describedAs("Job Id or -1 to resave all");
                accepts(RESAVE_WORKFLOW_ARG, "Resaves Workflow").withRequiredArg().ofType(Long.class)
                        .describedAs("Workflow Id or -1 to resave all");
                accepts(PREVIEW_WORKFLOW_ARG,
                        "Preview Workflow on Web, requires --" + URL_ARG
                                + " currently it should be: http://imafish.dynamic.ucsd.edu/cws/makepreview")
                                        .withRequiredArg().ofType(File.class).describedAs("Kepler .kar file");
                accepts(DESCRIPTION_ARG, "Description for WorkspaceFile").withRequiredArg()
                        .ofType(String.class);
                accepts(TYPE_ARG, "Type of WorkspaceFile").withRequiredArg().ofType(String.class);
                accepts(NAME_ARG,
                        "Sets name for Workspace file when used with --" + UPLOAD_FILE_ARG + " and --"
                                + REGISTER_FILE_ARG).withRequiredArg().ofType(String.class)
                                        .describedAs("WorkspaceFile name");
                accepts(REGISTER_JAR_ARG, "Path to Jar to register WorkspaceFiles").withRequiredArg()
                        .ofType(File.class).describedAs("Path to this jar");
                accepts(GET_JOB_ARG, "Gets job from service in JSON format, requires --" + URL_ARG)
                        .withRequiredArg().ofType(Long.class).describedAs("Job Id");
                accepts(GET_WORKSPACE_FILE_ARG,
                        "Gets WorkspaceFile from service in JSON format, requires --" + URL_ARG)
                                .withRequiredArg().ofType(Long.class)
                                .describedAs("WorkspaceFile Id or -1 for all");
                accepts(PROJECT_ARG, "Project name ie CRBS.  Used with --" + SYNC_WITH_CLUSTER_ARG)
                        .withRequiredArg().ofType(String.class);
                accepts(PORTALNAME_ARG, "Portal name ie SLASH portal Used with --" + SYNC_WITH_CLUSTER_ARG)
                        .withRequiredArg().ofType(String.class);
                accepts(PORTAL_URL_ARG,
                        "Portal url ie http://slashsegmentation.com Used with --" + SYNC_WITH_CLUSTER_ARG)
                                .withRequiredArg().ofType(String.class);
                accepts(HELP_EMAIL_ARG, "Help and reply to email address Used with --" + SYNC_WITH_CLUSTER_ARG)
                        .withRequiredArg().ofType(String.class);
                accepts(BCC_EMAIL_ARG, "Blind Carbon copy email address Used with --" + SYNC_WITH_CLUSTER_ARG)
                        .withRequiredArg().ofType(String.class);
                accepts(WORKSPACE_FILE_FAILED_ARG,
                        "Denotes whether workspacefile failed (true) or not (false).  Used with --"
                                + UPDATE_PATH_ARG).withRequiredArg().ofType(Boolean.class)
                                        .describedAs("false = success and true = failed");
                accepts(ERROR_EMAIL_ARG,
                        "Email to receive notifications if errors are encountered.  Used with --"
                                + SYNC_WITH_CLUSTER_ARG).withRequiredArg().ofType(String.class);
                accepts(HELP_ARG).forHelp();
            }
        };

        OptionSet optionSet = null;
        try {
            optionSet = parser.parse(args);
        } catch (OptionException oe) {
            System.err.println("\nThere was an error parsing arguments: " + oe.getMessage() + "\n\n");
            parser.printHelpOn(System.err);
            System.exit(1);
        }

        if (optionSet.has(HELP_ARG) || (!optionSet.has(SYNC_WITH_CLUSTER_ARG) && !optionSet.has(UPLOAD_WF_ARG))
                && !optionSet.has(EXAMPLE_JSON_ARG) && !optionSet.has(UPLOAD_FILE_ARG)
                && !optionSet.has(GET_WORKSPACE_FILE_INFO_ARG) && !optionSet.has(UPDATE_PATH_ARG)
                && !optionSet.has(REGISTER_FILE_ARG) && !optionSet.has(RESAVE_WORKSPACEFILE_ARG)
                && !optionSet.has(RESAVE_JOB_ARG) && !optionSet.has(RESAVE_WORKFLOW_ARG)
                && !optionSet.has(PREVIEW_WORKFLOW_ARG) && !optionSet.has(GEN_OLD_KEPLER_XML_ARG)
                && !optionSet.has(GET_JOB_ARG) && !optionSet.has(GET_WORKSPACE_FILE_ARG)
                && !optionSet.has(GET_WORKFLOW_ARG)) {
            System.out.println(PROGRAM_HELP + "\n");
            parser.printHelpOn(System.out);
            System.exit(0);
        }

        if (optionSet.has(EXAMPLE_JSON_ARG)) {

            renderExampleWorkflowsAndTasksAsJson();
            System.exit(0);
        }

        if (optionSet.has(GET_JOB_ARG)) {
            failIfOptionSetMissingURLOrLoginOrToken(optionSet, "--" + GET_JOB_ARG + " flag");
            getJobAsJson(optionSet);
            System.exit(0);
        }
        if (optionSet.has(GET_WORKSPACE_FILE_ARG)) {
            failIfOptionSetMissingURLOrLoginOrToken(optionSet, "--" + GET_WORKSPACE_FILE_ARG + " flag");
            getWorkspaceFileAsJson(optionSet);
            System.exit(0);
        }
        if (optionSet.has(GET_WORKFLOW_ARG)) {
            failIfOptionSetMissingURLOrLoginOrToken(optionSet, "--" + GET_WORKFLOW_ARG + " flag");
            getWorkflowAsJson(optionSet);
            System.exit(0);
        }

        MultivaluedMapFactory multivaluedMapFactory = new MultivaluedMapFactoryImpl();

        if (optionSet.has(GEN_OLD_KEPLER_XML_ARG)) {
            String workflowFileOrId = (String) optionSet.valueOf(GEN_OLD_KEPLER_XML_ARG);
            File workflowFile = new File(workflowFileOrId);
            Workflow w = null;

            //if value is a file attempt to load it as a workflow file
            if (workflowFile.exists() && workflowFile.isFile()) {
                w = getWorkflowFromFile(workflowFile);
                if (w == null) {
                    throw new Exception("Unable to extract workflow from file: " + workflowFile);
                }
            } else {
                //assume the value is a workflow id and get it from the service
                //but fail if url is missing
                failIfOptionSetMissingURLOrLoginOrToken(optionSet, "--" + GEN_OLD_KEPLER_XML_ARG + " flag");
                User u = getUserFromOptionSet(optionSet);
                WorkflowRestDAOImpl workflowDAO = new WorkflowRestDAOImpl();
                workflowDAO.setRestURL((String) optionSet.valueOf(URL_ARG));
                workflowDAO.setUser(u);
                w = workflowDAO.getWorkflowById(workflowFileOrId, u);
                if (w == null) {
                    throw new Exception("Unable to extract workflow from id: " + workflowFileOrId);
                }
            }

            VersionOneWorkflowXmlWriter xmlWriter = new VersionOneWorkflowXmlWriter();
            StringWriter sw = new StringWriter();
            xmlWriter.write(sw, w);
            System.out.println(sw.toString());
            System.exit(0);

        }

        if (optionSet.has(PREVIEW_WORKFLOW_ARG)) {
            failIfOptionSetMissingURL(optionSet, "--" + PREVIEW_WORKFLOW_ARG + " flag");

            File workflowFile = (File) optionSet.valueOf(PREVIEW_WORKFLOW_ARG);
            Workflow w = getWorkflowFromFile(workflowFile);
            if (w == null) {
                throw new Exception("Unable to extract workflow from file");
            }
            uploadPreviewWorkflowFile((String) optionSet.valueOf(URL_ARG), w);
            System.exit(0);
        }

        if (optionSet.has(REGISTER_FILE_ARG)) {
            addNewWorkspaceFile(optionSet, false, REGISTER_FILE_ARG);
            System.exit(0);
        }

        if (optionSet.has(RESAVE_WORKSPACEFILE_ARG)) {
            failIfOptionSetMissingURLOrLoginOrToken(optionSet, "--" + RESAVE_WORKSPACEFILE_ARG + " flag");
            WorkspaceFileRestDAOImpl workspaceFileDAO = new WorkspaceFileRestDAOImpl();
            User u = getUserFromOptionSet(optionSet);
            workspaceFileDAO.setUser(u);
            workspaceFileDAO.setRestURL((String) optionSet.valueOf(URL_ARG));
            Long workspaceId = (Long) optionSet.valueOf(RESAVE_WORKSPACEFILE_ARG);
            if (workspaceId == -1) {
                System.out.println("Resaving all workspace files");
                List<WorkspaceFile> wsfList = workspaceFileDAO.getWorkspaceFiles(null, null, null, null, null);
                if (wsfList != null) {
                    System.out.println("Found " + wsfList.size() + " workspace files to resave");
                    for (WorkspaceFile wsf : wsfList) {
                        System.out.println("WorkspaceFile Id: " + wsf.getId());
                        workspaceFileDAO.resave(wsf.getId());
                    }
                }
            } else {
                workspaceFileDAO.resave(workspaceId);
            }
            System.exit(0);
        }

        if (optionSet.has(RESAVE_JOB_ARG)) {
            failIfOptionSetMissingURLOrLoginOrToken(optionSet, "--" + RESAVE_JOB_ARG + " flag");
            JobRestDAOImpl jobDAO = new JobRestDAOImpl();
            User u = getUserFromOptionSet(optionSet);
            jobDAO.setUser(u);
            jobDAO.setRestURL((String) optionSet.valueOf(URL_ARG));
            Long jobId = (Long) optionSet.valueOf(RESAVE_JOB_ARG);
            if (jobId == -1) {
                System.out.println("Resaving all jobs");
                List<Job> jobList = jobDAO.getJobs(null, null, null, true, true, Boolean.TRUE);
                if (jobList != null) {
                    System.out.println("Found " + jobList.size() + " jobs to resave");
                    for (Job j : jobList) {
                        System.out.println("job id: " + j.getId());
                        jobDAO.resave(j.getId());
                    }
                }
            } else {
                jobDAO.resave(jobId);
            }
            System.exit(0);
        }

        if (optionSet.has(RESAVE_WORKFLOW_ARG)) {
            failIfOptionSetMissingURLOrLoginOrToken(optionSet, "--" + RESAVE_WORKFLOW_ARG + " flag");
            WorkflowRestDAOImpl workflowDAO = new WorkflowRestDAOImpl();
            User u = getUserFromOptionSet(optionSet);
            workflowDAO.setUser(u);
            workflowDAO.setRestURL((String) optionSet.valueOf(URL_ARG));
            Long workflowId = (Long) optionSet.valueOf(RESAVE_WORKFLOW_ARG);
            if (workflowId == -1) {
                System.out.println("Resaving all workflows");
                List<Workflow> workflowList = workflowDAO.getAllWorkflows(true, Boolean.TRUE);

                if (workflowList != null) {
                    System.out.println("Found " + workflowList.size() + " workflow(s) to resave");
                    for (Workflow w : workflowList) {
                        System.out.println("workflow id: " + w.getId());
                        workflowDAO.resave(w.getId());
                    }
                }
            } else {
                workflowDAO.resave(workflowId);
            }
            System.exit(0);
        }

        if (optionSet.has(UPDATE_PATH_ARG)) {
            failIfOptionSetMissingURLOrLoginOrToken(optionSet, "--" + UPDATE_PATH_ARG + " flag");

            User u = getUserFromOptionSet(optionSet);
            String workspaceId = (String) optionSet.valueOf(UPDATE_PATH_ARG);
            String path = null;
            if (optionSet.has(PATH_ARG)) {
                path = (String) optionSet.valueOf(PATH_ARG);
            }

            String size = null;
            if (optionSet.has(SIZE_ARG)) {
                size = ((Long) optionSet.valueOf(SIZE_ARG)).toString();
            }

            if (optionSet.has(MD5_ARG)) {
                //wsp.setMd5((String)optionSet.valueOf(MD5_ARG));
            }
            Boolean isFailed = null;

            if (optionSet.has(WORKSPACE_FILE_FAILED_ARG)) {
                isFailed = (Boolean) optionSet.valueOf(WORKSPACE_FILE_FAILED_ARG);
            }

            WorkspaceFileRestDAOImpl workspaceFileDAO = new WorkspaceFileRestDAOImpl();
            workspaceFileDAO.setUser(u);
            workspaceFileDAO.setRestURL((String) optionSet.valueOf(URL_ARG));
            workspaceFileDAO.updatePathSizeAndFailStatus(Long.parseLong(workspaceId), path, size, isFailed);

            System.exit(0);
        }

        if (optionSet.has(SYNC_WITH_CLUSTER_ARG)) {
            // @TODO NEED TO MAKE JOPT DO THIS REQUIRED FLAG CHECKING STUFF
            if (!optionSet.has(WF_EXEC_DIR_ARG)) {
                System.err.println(
                        "-" + WF_EXEC_DIR_ARG + " is required with -" + SYNC_WITH_CLUSTER_ARG + " flag");
                System.exit(2);
            }
            if (!optionSet.has(WF_DIR_ARG)) {
                System.err.println("-" + WF_DIR_ARG + " is required with -" + SYNC_WITH_CLUSTER_ARG + " flag");
                System.exit(3);
            }
            if (!optionSet.has(KEPLER_SCRIPT_ARG)) {
                System.err.println(
                        "-" + KEPLER_SCRIPT_ARG + " is required with -" + SYNC_WITH_CLUSTER_ARG + " flag");
                System.exit(4);
            }

            if (!optionSet.has(CAST_ARG)) {
                System.err.println("-" + CAST_ARG + " is required with -" + SYNC_WITH_CLUSTER_ARG + " flag");
                System.exit(5);
            }

            if (!optionSet.has(STAT_ARG)) {
                System.err.println("-" + STAT_ARG + " is required with -" + SYNC_WITH_CLUSTER_ARG + " flag");
                System.exit(6);
            }

            if (!optionSet.has(QUEUE_ARG)) {
                System.err.println("-" + QUEUE_ARG + " is required with -" + SYNC_WITH_CLUSTER_ARG + " flag");
                System.exit(7);
            }

            if (!optionSet.has(REGISTER_JAR_ARG)) {
                System.err.println(
                        "-" + REGISTER_JAR_ARG + " is required with -" + SYNC_WITH_CLUSTER_ARG + " flag");
                System.exit(8);
            }

            failIfOptionSetMissingLoginOrToken(optionSet, "--" + SYNC_WITH_CLUSTER_ARG + " flag");

            File castFile = (File) optionSet.valueOf(CAST_ARG);
            String castPath = castFile.getAbsolutePath();

            File statFile = (File) optionSet.valueOf(STAT_ARG);
            String statPath = statFile.getAbsolutePath();

            String queue = (String) optionSet.valueOf(QUEUE_ARG);

            File wfExecDir = (File) optionSet.valueOf(WF_EXEC_DIR_ARG);
            File wfDir = (File) optionSet.valueOf(WF_DIR_ARG);
            File keplerScript = (File) optionSet.valueOf(KEPLER_SCRIPT_ARG);

            String registerJar = null;
            if (optionSet.has(REGISTER_JAR_ARG)) {
                File registerJarFile = (File) optionSet.valueOf(REGISTER_JAR_ARG);
                registerJar = registerJarFile.getAbsolutePath();
            }
            JobEmailNotificationData emailNotifyData = getJobEmailNotificationData(optionSet);

            User u = getUserFromOptionSet(optionSet);

            ObjectifyService.ofy();
            String url = (String) optionSet.valueOf(SYNC_WITH_CLUSTER_ARG);
            JobRestDAOImpl jobDAO = new JobRestDAOImpl();
            jobDAO.setRestURL(url);
            jobDAO.setUser(u);

            System.out.println("Running sync with cluster");

            WorkspaceFileRestDAOImpl workspaceFileDAO = new WorkspaceFileRestDAOImpl();
            workspaceFileDAO.setRestURL(url);
            workspaceFileDAO.setUser(u);
            JobPath jobPath = new JobPathImpl(wfExecDir.getAbsolutePath());
            WorkspaceFilePathSetterImpl pathSetter = new WorkspaceFilePathSetterImpl(workspaceFileDAO);

            // Submit jobs to scheduler
            JobSubmissionManager submitter = new JobSubmissionManager(jobDAO, workspaceFileDAO, pathSetter,
                    jobPath, wfDir.getAbsolutePath(), keplerScript.getAbsolutePath(), castPath, queue, u, url,
                    registerJar, emailNotifyData);

            submitter.submitJobs();

            // Update job status for all jobs in system
            MapOfJobStatusFactoryImpl jobStatusFactory = new MapOfJobStatusFactoryImpl(statPath);

            WorkflowFailedParser workflowFailedParser = new WorkflowFailedParserImpl();
            JobStatusUpdater updater = new JobStatusUpdater(jobDAO, jobStatusFactory, workflowFailedParser,
                    jobPath);
            updater.updateJobs();

            System.exit(0);
        }

        if (optionSet.has(App.GET_WORKSPACE_FILE_INFO_ARG)) {
            failIfOptionSetMissingURLOrLoginOrToken(optionSet, "--" + GET_WORKSPACE_FILE_INFO_ARG + " flag");

            WorkspaceFileRestDAOImpl workspaceFileDAO = new WorkspaceFileRestDAOImpl();

            workspaceFileDAO.setRestURL((String) optionSet.valueOf(URL_ARG));

            List<WorkspaceFile> wsFiles = workspaceFileDAO
                    .getWorkspaceFilesById((String) optionSet.valueOf(GET_WORKSPACE_FILE_INFO_ARG), null);

            if (wsFiles != null) {
                ObjectMapper om = new ObjectMapper();
                ObjectWriter ow = om.writerWithDefaultPrettyPrinter();
                System.out.print("[");
                boolean first = true;
                for (WorkspaceFile wsf : wsFiles) {
                    if (first == false) {
                        System.out.println(",");
                    } else {
                        first = false;
                    }
                    System.out.print(ow.writeValueAsString(wsf));
                }
                System.out.println("]");
            } else {
                System.err.println("[]");
            }
            System.exit(0);
        }

        if (optionSet.has(UPLOAD_FILE_ARG)) {
            addNewWorkspaceFile(optionSet, true, UPLOAD_FILE_ARG);
            System.exit(0);
        }

        if (optionSet.has(UPLOAD_WF_ARG)) {

            Long parentWfId = null;

            String postURL = null;
            if (optionSet.has(URL_ARG)) {
                postURL = (String) optionSet.valueOf(URL_ARG);
                failIfOptionSetMissingLoginOrToken(optionSet,
                        "--" + UPLOAD_WF_ARG + " and --" + URL_ARG + " flag");
            }

            File workflowFile = (File) optionSet.valueOf(UPLOAD_WF_ARG);
            Workflow w = getWorkflowFromFile(workflowFile);
            if (w != null) {

                if (optionSet.has(OWNER_ARG)) {
                    w.setOwner((String) optionSet.valueOf(OWNER_ARG));
                }

                ObjectMapper om = new ObjectMapper();
                if (parentWfId != null) {
                    w.setId(parentWfId);
                }
                if (postURL == null) {
                    System.out.println("\n--- JSON Representation of Workflow ---");
                    ObjectWriter ow = om.writerWithDefaultPrettyPrinter();
                    System.out.println(ow.writeValueAsString(w));
                    System.out.flush();
                    System.out.println("---------------------------------------");

                } else {
                    postURL = new StringBuilder().append(postURL).append(Constants.SLASH)
                            .append(Constants.REST_PATH).append(Constants.SLASH)
                            .append(Constants.WORKFLOWS_PATH).toString();

                    ClientConfig cc = new DefaultClientConfig();
                    cc.getClasses().add(StringProvider.class);
                    cc.getClasses().add(MultiPartWriter.class);
                    Client client = Client.create(cc);

                    client.setFollowRedirects(true);
                    WebResource resource = client.resource(postURL);
                    String workflowAsJson = om.writeValueAsString(w);

                    User u = getUserFromOptionSet(optionSet);
                    client.addFilter(new HTTPBasicAuthFilter(u.getLogin(), u.getToken()));
                    MultivaluedMap queryParams = multivaluedMapFactory.getMultivaluedMap(u);

                    String response = resource.queryParams(queryParams).type(MediaType.APPLICATION_JSON_TYPE)
                            .entity(workflowAsJson).post(String.class);
                    Workflow workflowRes = om.readValue(response, Workflow.class);
                    ObjectWriter ow = om.writerWithDefaultPrettyPrinter();

                    if (workflowRes.getWorkflowFileUploadURL() == null) {
                        throw new Exception(
                                "No upload url found for workflow!!!" + ow.writeValueAsString(workflowRes));
                    }

                    uploadWorkflowFile(workflowRes, workflowFile);
                }
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        System.err.println("Caught Exception: " + ex.getMessage());

        System.exit(2);
    }

    System.exit(0);
}

From source file:de.uniwue.info2.main.CommandLineInterpreter.java

@SuppressWarnings("static-access")
public static void main(String[] args) {

    /*-------------------------------------------------------- */
    /*---------------SETTING TARGET LANGUAGE------------------ */
    /*-------------------------------------------------------- */
    LanguageFactory languageFactory = new LanguageFactory();

    CommandLine line = null;/*from  w w  w  . j a v a  2s .c o  m*/
    CommandLineParser parser = new BasicParser();
    Options options = new Options();
    // options to display in the help page
    Options options_short = new Options();

    // add help option
    Option help_option = new Option(HELP_OPTION_SHORT, HELP_OPTION, false, HELP_DESCRIPTION);
    options.addOption(help_option);
    options_short.addOption(help_option);
    // add extended help option
    Option help2_option = new Option(HELP2_OPTION_SHORT, HELP2_OPTION, false, HELP2_DESCRIPTION);
    options.addOption(help2_option);
    options_short.addOption(help2_option);
    // add optional operations option
    options.addOption(new Option(OPTIONAL_OPTION_SHORT, OPTIONAL_OPTION, false, OPTIONAL_DESCRIPTION));
    options.addOption(new Option(BIG_ENDIAN_OPTION_SHORT, BIG_ENDIAN_OPTION, false, BIG_ENDIAN_DESCRIPTION));
    options.addOption(
            new Option(LITTLE_ENDIAN_OPTION_SHORT, LITTLE_ENDIAN_OPTION, false, LITTLE_ENDIAN_DESCRIPTION));
    // add optional operations config option
    options.addOption(OptionBuilder.withLongOpt(OPTIONAL_FUNCTIONS_CONFIG_OPTION)
            .withArgName(OPTIONAL_FUNCTIONS_CONFIG_ARGUMENT)
            .withDescription(OPTIONAL_FUNCTIONS_CONFIG_DESCRIPTION).hasArg()
            .create(OPTIONAL_FUNCTIONS_CONFIG_SHORT));
    // add dsl option
    Option dsl_option = OptionBuilder.withLongOpt(DSL_OPTION).withArgName(DSL_ARGUMENT)
            .withDescription(DSL_DESCRIPTION).hasArg().isRequired().create(DSL_OPTION_SHORT);
    options.addOption(dsl_option);
    options_short.addOption(dsl_option);
    // add output-folder option
    Option output_option = OptionBuilder.withLongOpt(OUTPUT_OPTION).isRequired().withArgName(OUTPUT_ARGUMENT)
            .withDescription(OUTPUT_DESCRIPTION).hasArg().create(OUTPUT_OPTION_SHORT);
    options.addOption(output_option);
    options_short.addOption(output_option);

    // count possible language-specifications
    short optionCounter = 1;

    // get all possible language-specifications from language-factory and iterate through them
    List<LanguageSpecification> lSpecs = languageFactory.getAvailableLanguageSpecifications_();

    for (LanguageSpecification lSpec : lSpecs) {
        // get all possible unit-specifications for current language and iterate through them
        List<UnitTestLibrarySpecification> uSpecs = languageFactory
                .getAvailableUnitTestLibraries_(lSpec.getOptionName());
        String languageDescriptionAll = LANGUAGE_SPECIFICATION + lSpec.getLanguageName();
        String languageCounter = "s" + INDEX.format(optionCounter++);

        for (UnitTestLibrarySpecification uSpec : uSpecs) {
            // get all possible arithmetic-library-specifications for current language and iterate through
            // them
            List<ArithmeticLibrarySpecification> aSpecs = languageFactory
                    .getAvailableArithmeticLibraries_(lSpec.getOptionName());
            for (ArithmeticLibrarySpecification aSpec : aSpecs) {
                String languageDescription = "Generate unit-test for " + lSpec.getLanguageName() + "\n*["
                        + uSpec.getLibraryName() + " - " + uSpec.getVersion() + "]\n*[" + aSpec.getLibraryName()
                        + " - " + aSpec.getVersion() + "]";

                // if there is more than one option, generate suitable option-names and add them all to
                // commandline options
                if (uSpecs.size() > 1 || aSpecs.size() > 1) {
                    options.addOption(OptionBuilder
                            .withLongOpt(lSpec.getOptionName() + "_" + uSpec.getOptionName() + "_"
                                    + aSpec.getOptionName())
                            .withDescription(languageDescription).hasArg(false)
                            .create("s" + INDEX.format(optionCounter++)));
                } else {
                    // if there is only one option, use language-name as option-name
                    languageDescriptionAll = languageDescription;
                }
            }
            // add specifications to options
            options.addOption(OptionBuilder.withLongOpt(lSpec.getOptionName())
                    .withDescription(languageDescriptionAll).hasArg(false).create(languageCounter));
        }
    }

    /*-------------------------------------------------------- */
    /*-------------------PARSE USER INPUT--------------------- */
    /*-------------------------------------------------------- */
    try {
        // manual search for help-arguments
        for (String arg : args) {
            arg = arg.trim().replace("-", "");
            if (arg.equals(HELP_OPTION_SHORT) || arg.equals(HELP_OPTION)) {
                printHelp(options_short);
                return;
            }
            if (arg.equals(HELP2_OPTION_SHORT) || arg.equals(HELP2_OPTION)) {
                printExtendedHelp(options);
                return;
            }
        }

        // parse arguments   
        line = parser.parse(options, args);

        File dsl_file = null;
        File output_folder = null;
        File optional_config = null;
        Properties optional_operations = null;
        Boolean optional = false;
        Boolean little_endian = null;
        ArrayList<String> optional_exceptions = new ArrayList<String>();

        // if help-option found print help
        if (line.hasOption(HELP2_OPTION_SHORT) || args.length == 0) {
            printExtendedHelp(options);
            return;
        }

        // if help-option found print help
        if (line.hasOption(HELP_OPTION_SHORT) || args.length == 0) {
            System.out.println("\n");
            printHelp(options_short);
            return;
        }

        if (line.hasOption(OPTIONAL_OPTION_SHORT)) {
            optional = true;
        }

        if (line.hasOption(LITTLE_ENDIAN_OPTION)) {
            little_endian = true;
        }

        if (line.hasOption(BIG_ENDIAN_OPTION)) {
            little_endian = false;
        }

        // if dsl-option found, check if file exists and is readable
        // print help if error occurs
        if (line.hasOption(DSL_OPTION_SHORT)) {
            dsl_file = new File(line.getOptionValue(DSL_OPTION_SHORT));
            if (!dsl_file.exists()) {
                System.err.println("\n" + SEPERATOR + "\n" + "ERROR - DSL-file doesn't exist:\n" + dsl_file
                        + "\n" + SEPERATOR + "\n");
                printHelp(options_short);
                return;
            } else if (dsl_file.isDirectory()) {
                System.err.println("\n" + SEPERATOR + "\n" + "ERROR - DSL-file is a directory:\n" + dsl_file
                        + "\n" + SEPERATOR + "\n");
                printHelp(options_short);
                return;
            } else if (!dsl_file.canRead()) {
                System.err.println("\n" + SEPERATOR + "\n" + "ERROR - Need read-permission for DSL-file:\n"
                        + dsl_file + "\n" + SEPERATOR + "\n");
                printHelp(options_short);
                return;
            }
        }

        // if output-option found, check if folder exists and if write-permission was granted
        // print help if error occurs
        if (line.hasOption(OUTPUT_OPTION_SHORT)) {
            output_folder = new File(line.getOptionValue(OUTPUT_OPTION_SHORT));
            if (!output_folder.exists()) {
                System.err.println("\n" + SEPERATOR + "\n" + "ERROR - Output-folder doesn't exist:\n"
                        + output_folder + "\n" + SEPERATOR + "\n");
                printHelp(options_short);
                return;
            } else if (!output_folder.isDirectory()) {
                System.err.println("\n" + SEPERATOR + "\n" + "ERROR - Output-folder is not a directory:\n"
                        + output_folder + "\n" + SEPERATOR + "\n");
                printHelp(options_short);
                return;
            } else if (!output_folder.canWrite() || !output_folder.canRead()) {
                System.err.println("\n" + SEPERATOR + "\n" + "ERROR - Missing permissions for output-folder:\n"
                        + output_folder + "\n" + SEPERATOR + "\n");
                printHelp(options_short);
                return;
            }
        }

        if (line.hasOption(OPTIONAL_FUNCTIONS_CONFIG_SHORT)) {
            optional_config = new File(line.getOptionValue(OPTIONAL_FUNCTIONS_CONFIG_OPTION));
            if (!dsl_file.exists()) {
                System.err.println("\n" + SEPERATOR + "\n" + "ERROR - config-file doesn't exist:\n" + dsl_file
                        + "\n" + SEPERATOR + "\n");
                printExtendedHelp(options);
                return;
            } else if (dsl_file.isDirectory()) {
                System.err.println("\n" + SEPERATOR + "\n" + "ERROR - config-file is a directory:\n" + dsl_file
                        + "\n" + SEPERATOR + "\n");
                printExtendedHelp(options);
                return;
            } else if (!dsl_file.canRead()) {
                System.err.println("\n" + SEPERATOR + "\n" + "ERROR - Need read-permission for config-file:\n"
                        + dsl_file + "\n" + SEPERATOR + "\n");
                printExtendedHelp(options);
                return;
            }
        }

        if (optional_config != null) {
            optional_operations = new Properties();
            BufferedInputStream stream = new BufferedInputStream(new FileInputStream(optional_config));
            optional_operations.load(stream);
            stream.close();
            String optional_prop = optional_operations.getProperty("GENERATE_OPTIONAL");
            if (optional_prop != null) {
                if (optional_prop.trim().toLowerCase().equals("true")) {
                    optional = true;
                } else if (optional_prop.trim().toLowerCase().equals("false")) {
                    optional = false;
                } else if (!optional_prop.trim().isEmpty()) {
                    System.err.println("\n" + SEPERATOR + "\n"
                            + "ERROR - Syntax incorrect in config-file:\nUse \"true\" or \"false\" for \"GENERATE_OPTIONAL\"\n"
                            + SEPERATOR + "\n");
                    printExtendedHelp(options);
                    return;
                }
            }
            String exceptions = optional_operations.getProperty("EXCLUSIONS");
            if (exceptions != null) {
                for (String exc : optional_operations.getProperty("EXCLUSIONS").split(";")) {
                    optional_exceptions.add(exc.trim());
                }
            }
        }

        /*-------------------------------------------------------- */
        /*-------------------START GENERATING--------------------- */
        /*-------------------------------------------------------- */

        // instantiate generator for unit-tests
        TestcaseGenerator mainGenerator = new TestcaseGenerator(dsl_file, output_folder);

        boolean overrideDefaultSpecs = false;

        // check if user input contains a language-specifications
        // if user specified language, set overrideDefaultSpecs to true, so that only given specifications
        // are used
        for (int i = 1; i <= optionCounter; i++) {
            String opt = "s" + INDEX.format(i);
            if (line.hasOption(opt)) {
                LanguageSpecification targetSpecification = languageFactory
                        .getLanguageSpecification(options.getOption(opt).getLongOpt());
                String output = (GENERATING_DIALOG + targetSpecification.getLanguageName());

                // finally generate unit-test for current language-specification
                boolean successful = mainGenerator.generateUnitTest(targetSpecification, optional,
                        optional_exceptions, little_endian);

                if (successful) {
                    System.out.println(output + "\n--> Successfully generated.");
                } else {
                    System.err.println(output + "\n--> ERROR - see logfile");
                }
                overrideDefaultSpecs = true;
            }
        }

        // skip, if user already defined one language-specification
        // if user did not define language-specification, generate unit-tests for all
        // possible language-specifications (default)
        if (!overrideDefaultSpecs) {
            for (int i = 0; i < lSpecs.size(); i++) {
                LanguageSpecification specification = languageFactory
                        .getLanguageSpecification(lSpecs.get(i).getOptionName());

                String output = INDEX.format(i + 1) + " - " + GENERATING_DIALOG
                        + specification.getLanguageName();

                // finally generate unit-test for current language-specification
                boolean successful = mainGenerator.generateUnitTest(specification, optional,
                        optional_exceptions, little_endian);

                if (successful) {
                    System.out.println(output + "\n--> Successfully generated.");
                } else {
                    System.err.println(output + "\n--> ERROR - see logfile");
                }
            }
        }

    } catch (ParseException | IOException p) {
        System.err.println("\n" + SEPERATOR + "\n" + "ERROR - WRONG ARGUMENTS:\n" + p.getMessage() + "\n"
                + SEPERATOR + "\n");
        printHelp(options_short);
        System.out.println("\n");
    }
}

From source file:com.zimbra.perf.chart.ChartUtil.java

public static void main(String[] args) throws Exception {
    CommandLineParser clParser = new GnuParser();
    Options opts = getOptions();/*from   ww  w .jav a  2 s.  c  om*/
    try {
        CommandLine cl = clParser.parse(opts, args);

        if (cl.hasOption('h'))
            usage(opts);
        if (!cl.hasOption('s') && !cl.hasOption('d'))
            usage(opts, "-s and -d options are required");
        if (!cl.hasOption('s'))
            usage(opts, "Missing required -s option");

        if (!cl.hasOption('d'))
            usage(opts, "Missing required -d option");

        String[] confs = cl.getOptionValues(OPT_CONF);
        if (confs == null || confs.length == 0)
            usage(opts, "Missing --" + OPT_CONF + " option");
        File[] confFiles = new File[confs.length];
        for (int i = 0; i < confs.length; i++) {
            File conf = new File(confs[i]);
            if (!conf.exists()) {
                System.err.printf("Configuration file %s does not exist\n", conf.getAbsolutePath());
                System.exit(1);
            }
            confFiles[i] = conf;
        }

        String[] srcDirStrs = cl.getOptionValues(OPT_SRCDIR);
        if (srcDirStrs == null || srcDirStrs.length == 0)
            usage(opts, "Missing --" + OPT_SRCDIR + " option");
        List<File> srcDirsList = new ArrayList<File>(srcDirStrs.length);
        for (int i = 0; i < srcDirStrs.length; i++) {
            File srcDir = new File(srcDirStrs[i]);
            if (srcDir.exists())
                srcDirsList.add(srcDir);
            else
                System.err.printf("Source directory %s does not exist\n", srcDir.getAbsolutePath());
        }
        if (srcDirsList.size() < 1)
            usage(opts, "No valid source directory found");
        File[] srcDirs = new File[srcDirsList.size()];
        srcDirsList.toArray(srcDirs);

        String destDirStr = cl.getOptionValue(OPT_DESTDIR);
        if (destDirStr == null)
            usage(opts, "Missing --" + OPT_DESTDIR + " option");
        File destDir = new File(destDirStr);
        if (!destDir.exists()) {
            boolean created = destDir.mkdirs();
            if (!created) {
                System.err.printf("Unable to create destination directory %s\n", destDir.getAbsolutePath());
                System.exit(1);
            }
        }
        if (!destDir.canWrite()) {
            System.err.printf("Destination directory %s is not writable\n", destDir.getAbsolutePath());
            System.exit(1);
        }

        String title = cl.getOptionValue(OPT_TITLE);
        if (title == null)
            title = srcDirs[0].getAbsoluteFile().getName();

        Date startAt = parseTimestampOption(cl, opts, OPT_START_AT);
        Date endAt = parseTimestampOption(cl, opts, OPT_END_AT);
        Date aggStartAt = parseTimestampOption(cl, opts, OPT_AGGREGATE_START_AT);
        Date aggEndAt = parseTimestampOption(cl, opts, OPT_AGGREGATE_END_AT);

        boolean noSummary = cl.hasOption('n');
        ChartUtil app = new ChartUtil(confFiles, srcDirs, destDir, title, startAt, endAt, aggStartAt, aggEndAt,
                noSummary);
        app.doit();
    } catch (Exception e) {
        e.printStackTrace();
        System.err.println();
        usage(opts);
    }
}

From source file:com.cloudera.oryx.app.traffic.TrafficUtil.java

public static void main(String[] args) throws Exception {
    if (args.length < 3) {
        System.err.println("usage: TrafficUtil [hosts] [requestIntervalMS] [threads] [... other args]");
        return;//from  w w  w  .  j a  v  a 2s . c o  m
    }

    String[] hostStrings = COMMA.split(args[0]);
    Preconditions.checkArgument(hostStrings.length >= 1);
    int requestIntervalMS = Integer.parseInt(args[1]);
    Preconditions.checkArgument(requestIntervalMS >= 0);
    int numThreads = Integer.parseInt(args[2]);
    Preconditions.checkArgument(numThreads >= 1);

    String[] otherArgs = new String[args.length - 3];
    System.arraycopy(args, 3, otherArgs, 0, otherArgs.length);

    List<URI> hosts = Arrays.stream(hostStrings).map(URI::create).collect(Collectors.toList());

    int perClientRequestIntervalMS = numThreads * requestIntervalMS;

    Endpoints alsEndpoints = new Endpoints(ALSEndpoint.buildALSEndpoints());
    AtomicLong requestCount = new AtomicLong();
    AtomicLong serverErrorCount = new AtomicLong();
    AtomicLong clientErrorCount = new AtomicLong();
    AtomicLong exceptionCount = new AtomicLong();

    long start = System.currentTimeMillis();
    ExecUtils.doInParallel(numThreads, numThreads, true, i -> {
        RandomGenerator random = RandomManager.getRandom(Integer.toString(i).hashCode() ^ System.nanoTime());
        ExponentialDistribution msBetweenRequests;
        if (perClientRequestIntervalMS > 0) {
            msBetweenRequests = new ExponentialDistribution(random, perClientRequestIntervalMS);
        } else {
            msBetweenRequests = null;
        }

        ClientConfig clientConfig = new ClientConfig();
        PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
        connectionManager.setMaxTotal(numThreads);
        connectionManager.setDefaultMaxPerRoute(numThreads);
        clientConfig.property(ApacheClientProperties.CONNECTION_MANAGER, connectionManager);
        clientConfig.connectorProvider(new ApacheConnectorProvider());
        Client client = ClientBuilder.newClient(clientConfig);

        try {
            while (true) {
                try {
                    WebTarget target = client.target("http://" + hosts.get(random.nextInt(hosts.size())));
                    Endpoint endpoint = alsEndpoints.chooseEndpoint(random);
                    Invocation invocation = endpoint.makeInvocation(target, otherArgs, random);

                    long startTime = System.currentTimeMillis();
                    Response response = invocation.invoke();
                    try {
                        response.readEntity(String.class);
                    } finally {
                        response.close();
                    }
                    long elapsedMS = System.currentTimeMillis() - startTime;

                    int statusCode = response.getStatusInfo().getStatusCode();
                    if (statusCode >= 400) {
                        if (statusCode >= 500) {
                            serverErrorCount.incrementAndGet();
                        } else {
                            clientErrorCount.incrementAndGet();
                        }
                    }

                    endpoint.recordTiming(elapsedMS);

                    if (requestCount.incrementAndGet() % 10000 == 0) {
                        long elapsed = System.currentTimeMillis() - start;
                        log.info("{}ms:\t{} requests\t({} client errors\t{} server errors\t{} exceptions)",
                                elapsed, requestCount.get(), clientErrorCount.get(), serverErrorCount.get(),
                                exceptionCount.get());
                        for (Endpoint e : alsEndpoints.getEndpoints()) {
                            log.info("{}", e);
                        }
                    }

                    if (msBetweenRequests != null) {
                        int desiredElapsedMS = (int) Math.round(msBetweenRequests.sample());
                        if (elapsedMS < desiredElapsedMS) {
                            Thread.sleep(desiredElapsedMS - elapsedMS);
                        }
                    }
                } catch (Exception e) {
                    exceptionCount.incrementAndGet();
                    log.warn("{}", e.getMessage());
                }
            }
        } finally {
            client.close();
        }
    });
}

From source file:org.silverpeas.dbbuilder.DBBuilder.java

/**
 * @param args//from   w w w . j  a  va  2s .c o  m
 * @see
 */
public static void main(String[] args) {
    ClassPathXmlApplicationContext springContext = new ClassPathXmlApplicationContext(
            "classpath:/spring-jdbc-datasource.xml");
    try {
        // Ouverture des traces
        Date startDate = new Date();
        System.out.println(
                MessageFormat.format(messages.getString("dbbuilder.start"), DBBuilderAppVersion, startDate));
        console = new Console(DBBuilder.class);
        console.printMessage("*************************************************************");
        console.printMessage(
                MessageFormat.format(messages.getString("dbbuilder.start"), DBBuilderAppVersion, startDate));
        // Lecture des variables d'environnement  partir de dbBuilderSettings
        dbBuilderResources = FileUtil
                .loadResource("/org/silverpeas/dbBuilder/settings/dbBuilderSettings.properties");
        // Lecture des paramtres d'entre
        params = new CommandLineParameters(console, args);

        if (params.isSimulate() && DatabaseType.ORACLE == params.getDbType()) {
            throw new Exception(messages.getString("oracle.simulate.error"));
        }
        console.printMessage(messages.getString("jdbc.connection.configuration"));
        console.printMessage(ConnectionFactory.getConnectionInfo());
        console.printMessage("\tAction        : " + params.getAction());
        console.printMessage("\tVerbose mode  : " + params.isVerbose());
        console.printMessage("\tSimulate mode : " + params.isSimulate());
        if (Action.ACTION_CONNECT == params.getAction()) {
            // un petit message et puis c'est tout
            console.printMessage(messages.getString("connection.success"));
            System.out.println(messages.getString("connection.success"));
        } else {
            // Modules en place sur la BD avant install
            console.printMessage("DB Status before build :");
            List<String> packagesIntoDB = checkDBStatus();
            // initialisation d'un vecteur des instructions SQL  passer en fin d'upgrade
            // pour mettre  niveau les versions de modules en base
            MetaInstructions sqlMetaInstructions = new MetaInstructions();
            File dirXml = new File(params.getDbType().getDBContributionDir());
            DBXmlDocument destXml = loadMasterContribution(dirXml);
            UninstallInformations processesToCacheIntoDB = new UninstallInformations();

            File[] listeFileXml = dirXml.listFiles();
            Arrays.sort(listeFileXml);

            List<DBXmlDocument> listeDBXmlDocument = new ArrayList<DBXmlDocument>(listeFileXml.length);
            int ignoredFiles = 0;
            // Ouverture de tous les fichiers de configurations
            console.printMessage(messages.getString("ignored.contribution"));

            for (File xmlFile : listeFileXml) {
                if (xmlFile.isFile() && "xml".equals(FileUtil.getExtension(xmlFile))
                        && !(FIRST_DBCONTRIBUTION_FILE.equalsIgnoreCase(xmlFile.getName()))
                        && !(MASTER_DBCONTRIBUTION_FILE.equalsIgnoreCase(xmlFile.getName()))) {
                    DBXmlDocument fXml = new DBXmlDocument(dirXml, xmlFile.getName());
                    fXml.load();
                    // vrification des dpendances et prise en compte uniquement si dependences OK
                    if (hasUnresolvedRequirements(listeFileXml, fXml)) {
                        console.printMessage(
                                '\t' + xmlFile.getName() + " (because of unresolved requirements).");
                        ignoredFiles++;
                    } else if (ACTION_ENFORCE_UNINSTALL == params.getAction()) {
                        console.printMessage('\t' + xmlFile.getName() + " (because of "
                                + ACTION_ENFORCE_UNINSTALL + " mode).");
                        ignoredFiles++;
                    } else {
                        listeDBXmlDocument.add(fXml);
                    }
                }
            }
            if (0 == ignoredFiles) {
                console.printMessage("\t(none)");
            }

            // prpare une HashMap des modules prsents en fichiers de contribution
            Map packagesIntoFile = new HashMap();
            int j = 0;
            console.printMessage(messages.getString("merged.contribution"));
            console.printMessage(params.getAction().toString());
            if (ACTION_ENFORCE_UNINSTALL != params.getAction()) {
                console.printMessage('\t' + FIRST_DBCONTRIBUTION_FILE);
                j++;
            }
            for (DBXmlDocument currentDoc : listeDBXmlDocument) {
                console.printMessage('\t' + currentDoc.getName());
                j++;
            }
            if (0 == j) {
                console.printMessage("\t(none)");
            }
            // merge des diffrents fichiers de contribution ligibles :
            console.printMessage("Build decisions are :");
            // d'abord le fichier dbbuilder-contribution ...
            DBXmlDocument fileXml;
            if (ACTION_ENFORCE_UNINSTALL != params.getAction()) {
                try {
                    fileXml = new DBXmlDocument(dirXml, FIRST_DBCONTRIBUTION_FILE);
                    fileXml.load();
                } catch (Exception e) {
                    // contribution de dbbuilder non trouve -> on continue, on est certainement en train
                    // de desinstaller la totale
                    fileXml = null;
                }
                if (null != fileXml) {
                    DBBuilderFileItem dbbuilderItem = new DBBuilderFileItem(fileXml);
                    packagesIntoFile.put(dbbuilderItem.getModule(), null);
                    mergeActionsToDo(dbbuilderItem, destXml, processesToCacheIntoDB, sqlMetaInstructions);
                }
            }

            // ... puis les autres
            for (DBXmlDocument currentDoc : listeDBXmlDocument) {
                DBBuilderFileItem tmpdbbuilderItem = new DBBuilderFileItem(currentDoc);
                packagesIntoFile.put(tmpdbbuilderItem.getModule(), null);
                mergeActionsToDo(tmpdbbuilderItem, destXml, processesToCacheIntoDB, sqlMetaInstructions);
            }

            // ... et enfin les pices BD  dsinstaller
            // ... attention, l'ordonnancement n'tant pas dispo, on les traite dans
            // l'ordre inverse pour faire passer busCore a la fin, de nombreuses contraintes
            // des autres modules referencant les PK de busCore
            List<String> itemsList = new ArrayList<String>();

            boolean foundDBBuilder = false;
            for (String dbPackage : packagesIntoDB) {
                if (!packagesIntoFile.containsKey(dbPackage)) {
                    // Package en base et non en contribution -> candidat  desinstallation
                    if (DBBUILDER_MODULE.equalsIgnoreCase(dbPackage)) {
                        foundDBBuilder = true;
                    } else if (ACTION_ENFORCE_UNINSTALL == params.getAction()) {
                        if (dbPackage.equals(params.getModuleName())) {
                            itemsList.add(0, dbPackage);
                        }
                    } else {
                        itemsList.add(0, dbPackage);
                    }
                }
            }

            if (foundDBBuilder) {
                if (ACTION_ENFORCE_UNINSTALL == params.getAction()) {
                    if (DBBUILDER_MODULE.equals(params.getModuleName())) {
                        itemsList.add(itemsList.size(), DBBUILDER_MODULE);
                    }
                } else {
                    itemsList.add(itemsList.size(), DBBUILDER_MODULE);
                }
            }
            for (String item : itemsList) {
                console.printMessage("**** Treating " + item + " ****");
                DBBuilderDBItem tmpdbbuilderItem = new DBBuilderDBItem(item);
                mergeActionsToDo(tmpdbbuilderItem, destXml, processesToCacheIntoDB, sqlMetaInstructions);
            }
            destXml.setName("res.txt");
            destXml.save();
            console.printMessage("Build parts are :");
            // Traitement des pices slectionnes
            // remarque : durant cette phase, les erreurs sont traites -> on les catche en
            // retour sans les retraiter
            if (ACTION_INSTALL == params.getAction()) {
                processDB(destXml, processesToCacheIntoDB, sqlMetaInstructions, TAGS_TO_MERGE_4_INSTALL);
            } else if (ACTION_UNINSTALL == params.getAction()
                    || ACTION_ENFORCE_UNINSTALL == params.getAction()) {
                processDB(destXml, processesToCacheIntoDB, sqlMetaInstructions, TAGS_TO_MERGE_4_UNINSTALL);
            } else if (ACTION_OPTIMIZE == params.getAction()) {
                processDB(destXml, processesToCacheIntoDB, sqlMetaInstructions, TAGS_TO_MERGE_4_OPTIMIZE);
            } else if (ACTION_ALL == params.getAction()) {
                processDB(destXml, processesToCacheIntoDB, sqlMetaInstructions, TAGS_TO_MERGE_4_ALL);
            }
            // Modules en place sur la BD en final
            console.printMessage("Finally DB Status :");
            checkDBStatus();
        }
        Date endDate = new Date();
        console.printMessage(MessageFormat.format(messages.getString("dbbuilder.success"), endDate));
        System.out.println("*******************************************************************");
        System.out.println(MessageFormat.format(messages.getString("dbbuilder.success"), endDate));
    } catch (Exception e) {
        e.printStackTrace();
        console.printError(e.getMessage(), e);
        Date endDate = new Date();
        console.printError(MessageFormat.format(messages.getString("dbbuilder.failure"), endDate));
        System.out.println("*******************************************************************");
        System.out.println(MessageFormat.format(messages.getString("dbbuilder.failure"), endDate));
        System.exit(1);
    } finally {
        springContext.close();
        console.close();
    }
}