175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey/*******************************************************************************
275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey * Copyright (c) 2000, 2006 IBM Corporation and others.
375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey * All rights reserved. This program and the accompanying materials
475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey * are made available under the terms of the Eclipse Public License v1.0
575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey * which accompanies this distribution, and is available at
675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey * http://www.eclipse.org/legal/epl-v10.html
775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey *
875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey * Contributors:
975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey *     IBM Corporation - initial API and implementation
1075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey *******************************************************************************/
1175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeypackage org.eclipse.releng.generators;
1275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
1375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyimport java.util.ArrayList;
1475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyimport java.util.List;
1575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
1675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey/**
1775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey * Defines common behaviour for PDE Core applications.
1875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey */
1963d27a9233fed934340231f438493746084a681dJeff Sharkeypublic abstract class AbstractApplication {
2063d27a9233fed934340231f438493746084a681dJeff Sharkey
2163d27a9233fed934340231f438493746084a681dJeff Sharkey/**
2263d27a9233fed934340231f438493746084a681dJeff Sharkey * Starting point for application logic.
2363d27a9233fed934340231f438493746084a681dJeff Sharkey */
2463d27a9233fed934340231f438493746084a681dJeff Sharkeyprotected abstract void run() throws Exception;
2563d27a9233fed934340231f438493746084a681dJeff Sharkey
2663d27a9233fed934340231f438493746084a681dJeff Sharkey/*
27241dde2306202e7655fdf41d5381f2874e47e108Jeff Sharkey * @see IPlatformRunnable#run(Object)
28241dde2306202e7655fdf41d5381f2874e47e108Jeff Sharkey */
2975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeypublic Object run(Object args) throws Exception {
3075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey	processCommandLine(getArrayList((String[]) args));
3175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey	try {
3275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey		run();
3375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey	} catch (Exception e) {
3475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey		e.printStackTrace(System.out);
3575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey	}
36a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey	return null;
3775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey}
3875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
3975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey/**
4075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey * Helper method to ensure an array is converted into an ArrayList.
41a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey */
4275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeypublic static ArrayList getArrayList(Object[] args) {
4363d27a9233fed934340231f438493746084a681dJeff Sharkey	// We could be using Arrays.asList() here, but it does not specify
4463d27a9233fed934340231f438493746084a681dJeff Sharkey	// what kind of list it will return. We do need a list that
45a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey	// implements the method List.remove(int) and ArrayList does.
4663d27a9233fed934340231f438493746084a681dJeff Sharkey	ArrayList result = new ArrayList(args.length);
4775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey	for (int i = 0; i < args.length; i++)
4875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey		result.add(args[i]);
4975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey	return result;
50a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey}
5175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
5275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
5375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
5475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
5575279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey/**
5675279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey * Looks for interesting command line arguments.
5775279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey */
5875279904202357565cf5a1cb11148d01f42b4569Jeff Sharkeyprotected void processCommandLine(List commands) {
5975279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey}
6075279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey
6175279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey/**
6275279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey * From a command line list, get the array of arguments of a given parameter.
6375279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey * The parameter and its arguments are removed from the list.
6475279904202357565cf5a1cb11148d01f42b4569Jeff Sharkey * @return null if the parameter is not found or has no arguments
65a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey */
66a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkeyprotected String[] getArguments(List commands, String param) {
67a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey	int index = commands.indexOf(param);
68a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey	if (index == -1)
69a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey		return null;
70a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey	commands.remove(index);
71a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey	if (index == commands.size()) // if this is the last command
72a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey		return null;
73a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey	List args = new ArrayList(commands.size());
74a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey	while (index < commands.size()) { // while not the last command
75a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey		String command = (String) commands.get(index);
76a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey		if (command.startsWith("-")) // is it a new parameter?
77a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey			break;
78a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey		args.add(command);
79a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey		commands.remove(index);
80a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey	}
81a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey	if (args.isEmpty())
82a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey		return null;
83a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey	return (String[]) args.toArray(new String[args.size()]);
84a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey}
85a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey
86a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey
87a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey
88a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey
89a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey}
90a63ba59260cd1bb3f5c16e395ace45a61f1d4461Jeff Sharkey