📄 installer.java
字号:
* @param location the URL from which the application descriptor can be * updated. * @param jad application descriptor to install from * @param jadEncoding character encoding for the JAD or null for the * default encoding * @param jadCookie cookie that came with the JAD, can be null * will be sent with the JAR request * @param username for HTTP authentication, can be null * @param password for HTTP authentication, can be null if username is null * @param force if <code>true</code> the MIDlet suite components to be * installed will overwrite any existing components without * any version comparison. * @param installListener object to receive status updates and install * warnings, can be null * * @return the unique name of the installed package. * * @exception IOException is thrown if any error prevents the installation * of the MIDlet suite, including being unable to access the application * descriptor or JAR. * @exception InvalidJadException if the downloaded application descriptor * is invalid. * @exception SecurityException if the caller does not have permission * to install software. */ public String install(String location, byte[] jad, String jadEncoding, String jadCookie, String username, String password, boolean force, InstallListener installListener) throws IOException, InvalidJadException, SecurityException { state = new InstallStateImpl(); state.jadUrl = location; state.force = force; state.nextStep = 2; state.jad = jad; state.jadEncoding = jadEncoding; state.cookie = jadCookie; state.username = username; state.password = password; listener = installListener; return performInstall(); } /** * Same as {@link #install(String, boolean, InstallListener)} * but warnings are converted to exceptions. * * @param location the URL from which the application descriptor can be * retrieved. * @param force if <code>true</code> the MIDlet suite components to be * installed will overwrite any existing components without * any version comparison. * * @return the unique name of the installed package. * * @exception IOException is thrown if any error prevents the installation * of the MIDlet suite, including being unable to access the application * descriptor or JAR. * @exception InvalidJadException if the downloaded application descriptor * is invalid. * @exception SecurityException if the caller does not have permission * to install software. */ public String install(String location, boolean force) throws IOException, InvalidJadException, SecurityException { return install(location, force, null); } /** * Perform an install. * * @return the unique name of the installed package. * * @exception IOException is thrown, if an I/O error occurs during * descriptor or jar file download * @exception InvalidJadException is thrown, if the descriptor file is not * properly formatted or does not contain the required * information. */ private synchronized String performInstall() throws IOException, InvalidJadException { state.startTime = System.currentTimeMillis(); while (state.nextStep < 6) { /* * clear the previous warning, so we can tell if another has * happened */ state.exception = null; if (state.stopInstallation) { postStatusBackToProvider(USER_CANCELLED_MSG); throw new IOException("stopped"); } switch (state.nextStep) { case 1: installStep1(); break; case 2: installStep2(); break; case 3: installStep3(); break; case 4: installStep4(); break; case 5: installStep5(); break; } if (state.exception != null) { if (listener == null) { throw state.exception; } if (!listener.warnUser(state)) { state.stopInstallation = true; postStatusBackToProvider(USER_CANCELLED_MSG); throw state.exception; } } } return state.storageName; } /** * Download the JAD, * save it and any cookies in the install state. * * @exception IOException is thrown, if an I/O error occurs during * descriptor or jar file download * @exception InvalidJadException is thrown, if the descriptor file is not * properly formatted or does not contain the required */ private void installStep1() throws IOException, InvalidJadException { byte[] jad; if (state.jadUrl == null || state.jadUrl.length() == 0) { throw new IOException("Must specify URL of .jad file"); } try { jad = downloadJAD(); state.jad = jad; state.nextStep++; return; } catch (InvalidJadException ije) { if (ije.getReason() == InvalidJadException.UNAUTHORIZED) { state.exception = ije; return; } throw ije; } } /** * Parse the JAD, make sure it has * the required properties, and save them in the install state. * * @exception IOException is thrown, if an I/O error occurs during * descriptor or jar file download * @exception InvalidJadException is thrown, if the descriptor file is not * properly formatted or does not contain the required */ private void installStep2() throws IOException, InvalidJadException { MIDletSuite midletSuite; int cmpResult; state.jadProps = new JadProperties(); try { parseJad(new ByteArrayInputStream(state.jad), state.jadEncoding, state.jadProps); } catch (InvalidJadException ije) { postStatusBackToProvider(INVALID_JAD_MSG); throw ije; } state.suiteName = state.jadProps.getProperty(SUITE_NAME_PROP); if (state.suiteName == null || state.suiteName.length() == 0) { postStatusBackToProvider(INVALID_JAD_MSG); throw new InvalidJadException(InvalidJadException.MISSING_SUITE_NAME); } state.vendor = state.jadProps.getProperty(VENDOR_PROP); if (state.vendor == null || state.vendor.length() == 0) { postStatusBackToProvider(INVALID_JAD_MSG); throw new InvalidJadException(InvalidJadException.MISSING_VENDOR); } state.version = state.jadProps.getProperty(VERSION_PROP); if (state.version == null || state.version.length() == 0) { postStatusBackToProvider(INVALID_JAD_MSG); throw new InvalidJadException(InvalidJadException.MISSING_VERSION); } // Check if app already exists state.storageName = makeStorageName(state.vendor, state.suiteName); midletSuite = getMIDletSuite(state.storageName); try { checkVersionFormat(state.version); state.nextStep++; if (midletSuite == null) { // there is not previous version return; } state.isPreviousVersion = true; // get this now so we do not have to save the whole suite object state.previousJadUrl = midletSuite.getJadUrl(); // If it does, check version information cmpResult = vercmp(state.version, midletSuite.getProperty(VERSION_PROP)); if (cmpResult < 0) { // never install an older version, stop install throw new InvalidJadException(InvalidJadException.OLD_VERSION); } // Only check version if we aren't forcing an install (update) if (state.force) { return; } if (cmpResult > 0) { // new version, warn user state.exception = new InvalidJadException( InvalidJadException.NEW_VERSION); return; } // already installed, warn user state.exception = new InvalidJadException( InvalidJadException.ALREADY_INSTALLED); return; } catch (NumberFormatException nfe) { postStatusBackToProvider(INVALID_JAD_MSG); throw new InvalidJadException(InvalidJadException.INVALID_VERSION); } } /** * Verify the JAD, and if the * JAD belongs to an installed suite, check the URL against the * installed one. * * @exception IOException is thrown, if an I/O error occurs during * descriptor or jar file download * @exception InvalidJadException is thrown, if the descriptor file is not * properly formatted or does not contain the required */ private void installStep3() throws IOException, InvalidJadException { try { state.domainOwner = verifyJad(state.jad, state.jadProps); } catch (InvalidJadException ije) { postStatusBackToProvider(INVALID_JAD_MSG); throw ije; } state.nextStep++; /* * the presence of a domain owner means a secure installer * subclass verified this JAD's signature. So checking the URL is * a should not be done. */ // perform a domain check not a straight compare if (state.domainOwner == null && state.previousJadUrl != null) { HttpUrl old = new HttpUrl(state.previousJadUrl); HttpUrl current = new HttpUrl(state.jadUrl); if ((current.domain != null && old.domain == null) || (current.domain == null && old.domain != null) || (current.domain != null && old.domain != null && !current.domain.regionMatches(true, 0, old.domain, 0, old.domain.length()))) { /* * The jad is at new location, could be bad, * let the user decide */ state.exception = new InvalidJadException( InvalidJadException.JAD_MOVED, (state.previousJadUrl == null ? "none" : state.previousJadUrl)); return; } } } /** * Make sure the suite can fit in storage. * * @exception IOException is thrown, if an I/O error occurs during * descriptor or jar file download * @exception InvalidJadException is thrown, if the descriptor file is not * properly formatted or does not contain the required */ private void installStep4() throws IOException, InvalidJadException { String sizeString; int dataSize; int suiteSize; sizeString = state.jadProps.getProperty(JAR_SIZE_PROP); if (sizeString == null) { postStatusBackToProvider(INVALID_JAD_MSG); throw new InvalidJadException(InvalidJadException.MISSING_JAR_SIZE); } try { state.expectedJarSize = Integer.parseInt(sizeString); } catch (NumberFormatException e) { postStatusBackToProvider(INVALID_JAD_MSG); throw new InvalidJadException(InvalidJadException.INVALID_VALUE); } sizeString = state.jadProps.getProperty(DATA_SIZE_PROP); if (sizeString == null) { dataSize = 0; } else { try { dataSize = Integer.parseInt(sizeString); } catch (NumberFormatException e) { postStatusBackToProvider(INVALID_JAD_MSG); throw new InvalidJadException(InvalidJadException.INVALID_VALUE); } } /* * A suite is a jad + jar + manifest + url + data size. * lets say the manifest is the same size as the jad * since we do know at this point. the size is in bytes, * UTF-8 chars can be upto 3 bytes */ suiteSize = state.expectedJarSize + (state.jad.length * 2) + (state.jadUrl.length() * 3) + dataSize; state.file = new File(classSecurityDomain); if (suiteSize > state.file.getBytesAvailableForFiles()) { postStatusBackToProvider(INSUFFICIENT_MEM_MSG); // the size reported to the user should be in K and rounded up throw new InvalidJadException(InvalidJadException.INSUFFICIENT_STORAGE, Integer.toString((suiteSize + 1023)/ 1024)); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -