throw new BuildException("Your Ant property \"blackberry.home\" is invalid, it needs to point to the JDE directory (so that it finds the \"bin\\rapc.jar\" file within the ${blackberry.home} directory).");
throw new BuildException("Your Ant property \"blackberry.home\" [" + blackberryHome + "] is invalid, it needs to point to the JDE directory (so that it finds the \"bin\\rapc.jar\" file within the ${blackberry.home} directory).");
	public void finalize(File jadFile, File jarFile, Device device,			Locale locale, Environment env) 	{		String codName = jarFile.getName();		codName = codName.substring( 0, codName.length() - ".jar".length() );		String blackberryHome = env.getVariable("blackberry.home");		if ( blackberryHome == null ) {			throw new BuildException("You need to define the Ant property \"blackberry.home\" that points to the JDE directory.");		}				File rapcJarFile = env.resolveFile( "${blackberry.home}/bin/rapc.jar" );		if ( !rapcJarFile.exists() ) {			throw new BuildException("Your Ant property \"blackberry.home\" is invalid, it needs to point to the JDE directory (so that it finds the \"bin\\rapc.jar\" file within the ${blackberry.home} directory).");		}				// check if a MIDlet should be converted or whether a normal		// blackberry application is used:		String mainClassName = env.getVariable( "blackberry.main");		boolean usePolishGui = env.hasSymbol( "polish.usePolishGui" );		if (mainClassName == null && usePolishGui) {			boolean useDefaultPackage = env.hasSymbol( "polish.useDefaultPackage" );			if (useDefaultPackage) {				mainClassName = "MIDlet";			} else {				mainClassName= "de.enough.polish.blackberry.midlet.MIDlet";			}			// add JAD file to JAR, so that MIDlet.getAppProperty() works later onwards:			try {				File txtJadFile = new File( jadFile.getParent(), jadFile.getName().substring( 0, jadFile.getName().length() - ".jad".length() ) + ".txt");				FileUtil.copy( jadFile, txtJadFile );				JarUtil.addToJar(txtJadFile, jarFile, null, true );			} catch (IOException e) {				e.printStackTrace();				throw new BuildException("Unable to store JAD file in BlackBerry JAR file: " + e.toString() );			}		}		if (mainClassName != null) {			try {				/*				String[] entries = FileUtil.readTextFile( jadFile );				String[] newEntries = new String[ entries.length + 1 ];				System.arraycopy( entries, 0, newEntries, 0, entries.length );				newEntries[ entries.length ] = "RIM-MIDlet-Flags-1: 0";				*/				String iconUrl = env.getVariable("MIDlet-Icon");				if (iconUrl != null && iconUrl.length() > 1) {					iconUrl = iconUrl.substring( 1 );				} else {					iconUrl = "";				}				String[] newEntries = new String[]{						"MIDlet-Name: " + env.getVariable("MIDlet-Name"),						"MIDlet-Version: " + env.getVariable("MIDlet-Version"),						"MIDlet-Vendor: " + env.getVariable("MIDlet-Vendor"),						"MIDlet-Jar-URL: " + jarFile.getName(),						"MIDlet-Jar-Size: " + jarFile.length(),						"MicroEdition-Profile: MIDP-2.0",						"MicroEdition-Configuration: CLDC-1.1",						"MIDlet-1: " + env.getVariable("MIDlet-Name") + "," + iconUrl + ",",						"MIDlet-Icon: " + iconUrl,						"RIM-MIDlet-Flags-1: 0",						""				};				File rapcFile = new File( jadFile.getParent(), codName + ".rapc");				FileUtil.writeTextFile( rapcFile, newEntries );			} catch ( IOException e ) {				// TODO Auto-generated catch block				e.printStackTrace();			}		}		if (!this.verbose) {			this.verbose = "true".equals( env.getVariable("polish.blackberry.verbose") );		}		ArrayList commands = new ArrayList();		try {			// call the rapc compiler for converting the JAR to a COD file:			commands.add( "java" );			commands.add( "-cp" );			commands.add( rapcJarFile.getAbsolutePath() );			commands.add( "net.rim.tools.compiler.Compiler" );			commands.add( "import=" +  blackberryHome + File.separatorChar + "lib" + File.separatorChar + "net_rim_api.jar" );			commands.add(  "codename=" + codName );			//commands.add( "-midlet" );			if (mainClassName == null) {				commands.add( "-midlet" );				commands.add( "jad=" + jadFile.getName() );			} else {				commands.add( codName + ".rapc" );			}			commands.add( jarFile.getName() );			System.out.println("rapc: Converting jar to cod for device [" + device.getIdentifier() + "]");			/*			Class compilerClass = Class.forName("net.rim.tools.compiler.Compiler");			Method mainMethod = compilerClass.getMethod("main", new Class[]{ String[].class } );			String[] args = (String[]) commands.toArray( new String[ commands.size() ]);			try {				mainMethod.invoke( null, new Object[]{ args } );			} catch ( InvocationTargetException e ) {				// the Compiler class calls System.exit() after the successfull 			}			*///			Object[] args = commands.toArray();//			StringBuffer argsBuffer = new StringBuffer();//			for (int i = 0; i < args.length; i++) {//				Object object = args[i];//				argsBuffer.append( object ).append(" ");//			}//			System.out.println("Call to rapc: " + argsBuffer.toString() );			File distDir = jarFile.getParentFile();			int result = ProcessUtil.exec( commands, "rapc: ", true, this, distDir );						if ( result != 0 ) {				System.err.println("rapc-call: " + commands.toString() );				throw new BuildException("rapc failed and returned " + result );			}			File csoFile = new File( distDir, codName + ".cso" );			File debugFile = new File( distDir, codName + ".debug" );			csoFile.delete();			debugFile.delete();						// now create an ALX file for deployment using the BlackBerry Desktop Manager:			ArrayList lines = new ArrayList();			lines.add( "<loader version=\"1.0\">");			lines.add( "<application id=\"" + codName + "\">" );			lines.add( "<name >" );			lines.add( env.getVariable("MIDlet-Name") );			lines.add( "</name>" );			String value = env.getVariable("MIDlet-Description");			if (value == null) {				value = "An application build with J2ME Polish.";			}			lines.add( "<description >" );			lines.add( value );			lines.add( "</description>" );			lines.add( "<version >" );			lines.add( env.getVariable("MIDlet-Version") );			lines.add( "</version>" );			lines.add( "<vendor >" );			lines.add( env.getVariable("MIDlet-Vendor") );			lines.add( "</vendor>" );			value = env.getVariable("MIDlet-Copyright");			lines.add( "<copyright >" );			if ( value != null ) {				lines.add( value );			} else {				lines.add( "Copyright (c) " + Calendar.getInstance().get( Calendar.YEAR ) + " " +  env.getVariable("MIDlet-Vendor") );			}			lines.add( "</copyright>" );			lines.add( "<fileset Java=\"1.0\">" );			lines.add( "<directory >" );			lines.add( "</directory>" );			lines.add( "<files >"  );			lines.add( codName + ".cod" );			lines.add( "</files>" );			lines.add( "</fileset>" );			lines.add( "</application>" );						lines.add( "</loader>");			File alxFile = new File( jarFile.getParentFile(),  codName  + ".alx" );			FileUtil.writeTextFile(alxFile, lines);						// request signature when the "blackberry.certificate.dir" variable is set:			if ( env.getVariable("blackberry.certificate.dir") != null) {				SignatureRequester requester = new SignatureRequester();				File codFile = new File( jarFile.getParent(), codName + ".cod" );				try {					int signResult = requester.requestSignature(device, locale, new File( blackberryHome ), codFile, env);					if (signResult != 0) {						System.out.println("Unable to request BlackBerry signature: Signing request returned " + signResult );					}				} catch (Exception e) {				    throw new BuildException("Unable to to request BlackBerry signature: " + e.toString() );				}			}		} catch (BuildException e) {			throw e;		} catch (Exception e){		    e.printStackTrace();			System.err.println("rapc-call: " + commands.toString() );		    throw new BuildException("rapc was unable to to transform JAR file: " + e.toString() );		}    }