15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/******************************************************************************* 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Copyright (c) 2000, 2006 IBM Corporation and others. 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * All rights reserved. This program and the accompanying materials 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * are made available under the terms of the Eclipse Public License v1.0 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * which accompanies this distribution, and is available at 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * http://www.eclipse.org/legal/epl-v10.html 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Contributors: 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * IBM Corporation - initial API and implementation 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *******************************************************************************/ 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)package org.eclipse.releng.generators; 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)import java.util.ArrayList; 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)import java.util.List; 152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/** 172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * Defines common behaviour for PDE Core applications. 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */ 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)public abstract class AbstractApplication { 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/** 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Starting point for application logic. 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */ 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)protected abstract void run() throws Exception; 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/* 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * @see IPlatformRunnable#run(Object) 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */ 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)public Object run(Object args) throws Exception { 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) processCommandLine(getArrayList((String[]) args)); 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) try { 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) run(); 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } catch (Exception e) { 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) e.printStackTrace(System.out); 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return null; 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/** 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Helper method to ensure an array is converted into an ArrayList. 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */ 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)public static ArrayList getArrayList(Object[] args) { 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // We could be using Arrays.asList() here, but it does not specify 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // what kind of list it will return. We do need a list that 452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // implements the method List.remove(int) and ArrayList does. 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ArrayList result = new ArrayList(args.length); 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) for (int i = 0; i < args.length; i++) 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) result.add(args[i]); 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return result; 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 55/** 56 * Looks for interesting command line arguments. 57 */ 58protected void processCommandLine(List commands) { 59} 60 61/** 62 * From a command line list, get the array of arguments of a given parameter. 63 * The parameter and its arguments are removed from the list. 64 * @return null if the parameter is not found or has no arguments 65 */ 66protected String[] getArguments(List commands, String param) { 67 int index = commands.indexOf(param); 68 if (index == -1) 69 return null; 70 commands.remove(index); 71 if (index == commands.size()) // if this is the last command 72 return null; 73 List args = new ArrayList(commands.size()); 74 while (index < commands.size()) { // while not the last command 75 String command = (String) commands.get(index); 76 if (command.startsWith("-")) // is it a new parameter? 77 break; 78 args.add(command); 79 commands.remove(index); 80 } 81 if (args.isEmpty()) 82 return null; 83 return (String[]) args.toArray(new String[args.size()]); 84} 85 86 87 88 89} 90