/**
  * Selects application uid from Nokia-MIDlet-UID attribute.
  *
  * @return uid if attribute specifies it, null otherwise.
  * @throws InstallerException if attribute exists and is not valid or specified uid is already in
  *     use.
  */
 private Uid selectAppUidFromAttribute(InstallBall aBall, int aIndex) {
   Uid uid = null;
   String appUidAttrName = MIDLET_UID_ATTR_NAME + aIndex;
   String appUidAttrValue = aBall.getAttributeValue(appUidAttrName);
   if (appUidAttrValue != null) {
     uid = PlatformUid.createUid(appUidAttrValue);
     if (uid == null) {
       Log.logError("Invalid " + appUidAttrName + " attribute value: " + appUidAttrValue);
       throw new InvalidAttributeException(
           InstallerErrorMessage.INST_CORRUPT_PKG,
           null,
           InstallerDetailedErrorMessage.ATTR_BAD_SYNTAX,
           new String[] {appUidAttrName},
           (aBall.attributeExistsInJad(appUidAttrName)
               ? OtaStatusCode.INVALID_DESCRIPTOR
               : OtaStatusCode.INVALID_JAR));
     }
     boolean uidInJad = aBall.attributeExistsInJad(appUidAttrName);
     // If jar file is not yet present, assume that uid
     // will be present in jar.
     boolean uidInJar = true;
     if (aBall.iJarFilename != null) {
       uidInJar = aBall.attributeExistsInJar(appUidAttrName);
     }
     if (uidInJar && !uidInJad && aBall.iJadAttributes != null) {
       // If Uid is specified in jar but not in jad
       // it must be ignored. Getting applications
       // through Java Verified process requires
       // that applications using protected Uids
       // can be installed as untrusted (without jad).
       Log.logWarning(appUidAttrName + " attribute present in jar but not in jad, ignoring it");
       uid = null;
     }
     if (uid != null) {
       // Check that uid is valid.
       PlatformUid.checkValidity(
           appUidAttrName, uid, aBall.iSuite.isTrusted(), uidInJad, uidInJar);
       // Check that uid is not already in use.
       if (appUidInUse(uid, aBall) || appUidInUse(uid, aBall.iSuite)) {
         Log.logError("Uid " + uid + " is already in use");
         throw new InvalidAttributeException(
             InstallerErrorMessage.INST_CORRUPT_PKG,
             null,
             InstallerDetailedErrorMessage.ATTR_HANDLING_FAILED,
             new String[] {appUidAttrName},
             (aBall.attributeExistsInJad(appUidAttrName)
                 ? OtaStatusCode.INVALID_DESCRIPTOR
                 : OtaStatusCode.INVALID_JAR));
       }
     }
     aBall.log("Selected app uid from " + appUidAttrName + " attribute: " + uid);
   }
   return uid;
 }
 /**
  * Checks that all specified Nokia-MIDlet-UID-n attributes are present in jar for trusted
  * application. Throws InstallerException if attributes are not present.
  */
 private static void checkNokiaMidletUids(InstallBall aBall) {
   if (aBall == null || !aBall.iSuite.isTrusted()) {
     return;
   }
   for (int i = 1; true; i++) {
     // Check if uid is specified with Nokia-MIDlet-UID attribute.
     String appUidAttrName = MIDLET_UID_ATTR_NAME + i;
     String appUidAttrValue = aBall.getAttributeValue(appUidAttrName);
     if (appUidAttrValue != null) {
       // Uid is specified with attribute, check that attribute
       // is present in jar.
       if (!aBall.attributeExistsInJar(appUidAttrName)) {
         Log.logError("For trusted application Uid must be specified in Jar: " + appUidAttrName);
         throw new InvalidAttributeException(
             InstallerErrorMessage.INST_CORRUPT_PKG,
             null,
             InstallerDetailedErrorMessage.ATTR_HANDLING_FAILED,
             new String[] {appUidAttrName},
             OtaStatusCode.INVALID_JAR);
       }
     } else {
       break;
     }
   }
 }
 /** Selects application uids. */
 private void selectApplicationUids(InstallBall ball) {
   // Select application uids.
   int startIndex = ball.iSuite.getApplications().size() + 1;
   Log.log("Selecting application uids, startIndex " + startIndex);
   for (int i = startIndex; true; i++) {
     String appAttrName = "MIDlet-" + i;
     String appAttrValue = ball.getAttributeValue(appAttrName);
     if (appAttrValue != null) {
       Uid uid = null;
       // Split MIDlet-n attribute into tokens.
       String[] tokens = Tokenizer.split(appAttrValue, ",");
       if (tokens == null || tokens.length != 3) {
         Log.logError("Invalid " + appAttrName + " attribute value: " + appAttrValue);
         throw new InvalidAttributeException(
             InstallerErrorMessage.INST_CORRUPT_PKG,
             null,
             InstallerDetailedErrorMessage.ATTR_BAD_SYNTAX,
             new String[] {appAttrName},
             (ball.attributeExistsInJad(appAttrName)
                 ? OtaStatusCode.INVALID_DESCRIPTOR
                 : OtaStatusCode.INVALID_JAR));
       }
       if (ball.iConversionInstallation && iUidsFromFile != null && iUidsFromFile.length > i) {
         // Conversion installation, use uid from 'uids' file.
         uid = iUidsFromFile[i];
         ball.log("Selected app uid from file: " + uid);
       }
       if (uid == null) {
         // Check if uid is specified with Nokia-MIDlet-UID attribute.
         uid = selectAppUidFromAttribute(ball, i);
       }
       if (uid == null && ball.iOldSuite != null) {
         // No uid yet, check from old suite.
         Vector oldApps = ball.iOldSuite.getApplications();
         if (oldApps.size() >= i) {
           // Use old application uid.
           uid = ((ApplicationInfo) oldApps.elementAt(i - 1)).getUid();
           // Check that uid is not already in use in this
           // suite. This could happen if Nokia-MIDlet-UID
           // attribute was used to select uid for different
           // app in the previous version of this suite.
           if (appUidInUse(uid, ball.iSuite)) {
             // Uid is already in use in this suite,
             // it cannot be used twice.
             Log.log("Old uid already in use: " + uid);
             uid = null;
           }
           if (uid != null) {
             ball.log("Selected old app uid: " + uid);
           }
         }
       }
       if (uid == null) {
         // If no uid chosen yet, then allocate a new application uid.
         uid = allocateUid(ball, tokens[0].trim());
         ball.log("Allocated new app uid: " + uid);
       }
       // Add a new ApplicationInfo to suite.
       ball.iSuite.addApplication(
           new ApplicationInfo(
               uid,
               tokens[0].trim(),
               tokens[1].trim(),
               tokens[2].trim(),
               ApplicationInfo.AUTOSTART_FALSE));
     } else {
       break;
     }
   }
 }