system/src/sc/layer/LayeredSystem.java
private boolean loadTypeIndex(Set<String> refreshedLayers) {
      String[] runtimeDirNames = getRuntimeDirNames(true);

      if (peerMode)
         System.err.println("*** TypeIndexEntry should only be loaded in the main layeredSystem");

      long startTime = System.currentTimeMillis();

      // A simple guard to prevent this from being done twice - the first one to create the process map should init the index.
      acquireDynLock(false);
      try {
         clearLeafLayerNames();
         if (typeIndexProcessMap == null) {
            typeIndexProcessMap = new ConcurrentHashMap<String, SysTypeIndex>();
            if (typeIndex == null) {
               typeIndex = new SysTypeIndex(this, getTypeIndexIdent());
            }
         }
         else
            return true;
      }
      finally {
         releaseDynLock(false);
      }

      for (String runtimeDirName:runtimeDirNames) {
         if (!runtimeDirName.startsWith(TYPE_INDEX_DIR_PREFIX))
            continue;

         String typeIndexIdent = runtimeDirName.substring(TYPE_INDEX_DIR_PREFIX.length());

         File typeIndexDir = new File(getTypeIndexDir(typeIndexIdent));
         String[] fileNames = typeIndexDir.list();
         if (fileNames == null) {
            error("*** Load typeIndex with no typeIndex directory: " + typeIndexDir.getPath());
            return true;
         }
         // We may create types_java directories for this layered system before we've used it for a specific process
         if (fileNames.length == 0)
            continue;
         HashMap<String,Boolean> filesToProcess = new HashMap<String,Boolean>();
         for (String file:fileNames) {
            if (file.equals(MAIN_SYSTEM_MARKER_FILE))
               continue;
            filesToProcess.put(file, Boolean.FALSE);
         }

         LayeredSystem curSys = findSystemFromTypeIndexIdent(typeIndexIdent);

         SysTypeIndex curTypeIndex = typeIndexProcessMap.get(typeIndexIdent);
         if (curTypeIndex == null && curSys != null && curSys.typeIndex != null) {
            curTypeIndex = curSys.typeIndex;
            typeIndexProcessMap.put(typeIndexIdent, curTypeIndex);
         }
         if (curTypeIndex == null) {
            typeIndexProcessMap.put(typeIndexIdent, curTypeIndex = new SysTypeIndex(curSys, typeIndexIdent));
         }
         if (curSys != null && curSys.typeIndex == null)
            curSys.typeIndex = curTypeIndex;

         boolean foundIndex = curTypeIndex.loadFromDir(getTypeIndexDir(typeIndexIdent));
         if (!foundIndex) {
            warning("Failed to load type index order file - rebuilding the type index");
            options.typeIndexMode = TypeIndexMode.Rebuild;
         }

         RefreshTypeIndexContext refreshCtx = new RefreshTypeIndexContext(typeIndexIdent, filesToProcess, curTypeIndex, refreshedLayers, typeIndexDir);

         while (filesToProcess.size() > 0) {
            String indexFileName = filesToProcess.keySet().iterator().next();

            if (indexFileName.endsWith(Layer.TYPE_INDEX_SUFFIX)) {
               String uname = indexFileName.substring(0, indexFileName.length() - Layer.TYPE_INDEX_SUFFIX.length());
               String layerName = Layer.getLayerNameFromUniqueUnderscoreName(uname);

               Layer layer = curSys == null ? null : curSys.lookupLayerSync(layerName, false, true);
               if (layer != null) {
                  filesToProcess.remove(indexFileName);

                  // TODO: do we need to refresh this layer's type index here?   Or did it already get refreshed?
                  LayeredSystem layerSys = layer.layeredSystem;
                  layerSys.acquireDynLock(false);
                  try {
                     SysTypeIndex sysIdx = layerSys.typeIndex;
                     LayerListTypeIndex idx = layer.activated ? sysIdx.activeTypeIndex : sysIdx.inactiveTypeIndex;
                     if (writeLocked == 0) {
                        System.err.println("*** Modifying type index without write lock");
                        new Throwable().printStackTrace();
                     }
                     if (layer.layerTypeIndex == null)
                        layer.initTypeIndex();
                     idx.addLayerTypeIndex(layerName, layer.layerTypeIndex);
                     refreshedLayers.add(layerName);
                  }
                  finally {
                     layerSys.releaseDynLock(false);
                  }
               }
               else {
                  acquireDynLock(false);
                  try {
                     boolean res;
                     if (curSys != null)
                        res = curSys.refreshLayerTypeIndexFile(layerName, refreshCtx, false, startTime);
                     else
                        res = this.refreshLayerTypeIndexFile(layerName, refreshCtx, false, startTime);

                     if (!res)
                        return false;

                     // The process of getting a layer may have created this runtime so use it as soon as it becomes available.
                     if (curSys == null)
                        curSys = findSystemFromTypeIndexIdent(typeIndexIdent);
                  }
                  finally {
                     releaseDynLock(false);
                  }
               }
            }
            else {
               filesToProcess.remove(indexFileName);
            }

            if (!checkIndexProgress(startTime))
               return false;
         }
         if (!rebuildLayersTypeIndex(refreshCtx.layersToRebuild, startTime)) {
            return false;
         }

         for (LayerTypeIndex startIndex:refreshCtx.toStartLaterIndexes) {
            for (BodyTypeDeclaration toStartLater:startIndex.toStartLaterTypes) {
               JavaModel toStartModel = toStartLater.getJavaModel();
               Layer toStartLayer = toStartModel.getLayer();
               if (toStartLayer.closed)
                  toStartLayer.markClosed(false, false);
               ParseUtil.initAndStartComponent(toStartModel);
            }
         }
      }

      /*
      if (!peerMode && peerSystems != null) {
         for (LayeredSystem peerSys:peerSystems) {
            peerSys.loadTypeIndex(refreshedLayers);
         }
      }
      */
      return true;
   }
/**
    * The internal method to load the main type index.  In general, we do not need to be called with the dyn lock and
    * it's best if you avoid doing that because this can take a long time when rebuilding the index from scratch.
    */
The internal method to load the main type index.  In general, we do not need to be called with the dyn lock and
it's best if you avoid doing that because this can take a long time when rebuilding the index from scratch.
The internal method to load the main type index . In general we do not need to be called with the dyn lock and it ' s best if you avoid doing that because this can take a long time when rebuilding the index from scratch .
private boolean loadTypeIndex(Set<String> refreshedLayers) {
      String[] runtimeDirNames = getRuntimeDirNames(true);
      if (peerMode)
         System.err.println("*** TypeIndexEntry should only be loaded in the main layeredSystem");
      long startTime = System.currentTimeMillis();
      acquireDynLock(false);
      try {
         clearLeafLayerNames();
         if (typeIndexProcessMap == null) {
            typeIndexProcessMap = new ConcurrentHashMap<String, SysTypeIndex>();
            if (typeIndex == null) {
               typeIndex = new SysTypeIndex(this, getTypeIndexIdent());
            }
         }
         else
            return true;
      }
      finally {
         releaseDynLock(false);
      }
      for (String runtimeDirName:runtimeDirNames) {
         if (!runtimeDirName.startsWith(TYPE_INDEX_DIR_PREFIX))
            continue;
         String typeIndexIdent = runtimeDirName.substring(TYPE_INDEX_DIR_PREFIX.length());
         File typeIndexDir = new File(getTypeIndexDir(typeIndexIdent));
         String[] fileNames = typeIndexDir.list();
         if (fileNames == null) {
            error("*** Load typeIndex with no typeIndex directory: " + typeIndexDir.getPath());
            return true;
         }
         if (fileNames.length == 0)
            continue;
         HashMap<String,Boolean> filesToProcess = new HashMap<String,Boolean>();
         for (String file:fileNames) {
            if (file.equals(MAIN_SYSTEM_MARKER_FILE))
               continue;
            filesToProcess.put(file, Boolean.FALSE);
         }
         LayeredSystem curSys = findSystemFromTypeIndexIdent(typeIndexIdent);
         SysTypeIndex curTypeIndex = typeIndexProcessMap.get(typeIndexIdent);
         if (curTypeIndex == null && curSys != null && curSys.typeIndex != null) {
            curTypeIndex = curSys.typeIndex;
            typeIndexProcessMap.put(typeIndexIdent, curTypeIndex);
         }
         if (curTypeIndex == null) {
            typeIndexProcessMap.put(typeIndexIdent, curTypeIndex = new SysTypeIndex(curSys, typeIndexIdent));
         }
         if (curSys != null && curSys.typeIndex == null)
            curSys.typeIndex = curTypeIndex;
         boolean foundIndex = curTypeIndex.loadFromDir(getTypeIndexDir(typeIndexIdent));
         if (!foundIndex) {
            warning("Failed to load type index order file - rebuilding the type index");
            options.typeIndexMode = TypeIndexMode.Rebuild;
         }
         RefreshTypeIndexContext refreshCtx = new RefreshTypeIndexContext(typeIndexIdent, filesToProcess, curTypeIndex, refreshedLayers, typeIndexDir);
         while (filesToProcess.size() > 0) {
            String indexFileName = filesToProcess.keySet().iterator().next();
            if (indexFileName.endsWith(Layer.TYPE_INDEX_SUFFIX)) {
               String uname = indexFileName.substring(0, indexFileName.length() - Layer.TYPE_INDEX_SUFFIX.length());
               String layerName = Layer.getLayerNameFromUniqueUnderscoreName(uname);
               Layer layer = curSys == null ? null : curSys.lookupLayerSync(layerName, false, true);
               if (layer != null) {
                  filesToProcess.remove(indexFileName);
                  LayeredSystem layerSys = layer.layeredSystem;
                  layerSys.acquireDynLock(false);
                  try {
                     SysTypeIndex sysIdx = layerSys.typeIndex;
                     LayerListTypeIndex idx = layer.activated ? sysIdx.activeTypeIndex : sysIdx.inactiveTypeIndex;
                     if (writeLocked == 0) {
                        System.err.println("*** Modifying type index without write lock");
                        new Throwable().printStackTrace();
                     }
                     if (layer.layerTypeIndex == null)
                        layer.initTypeIndex();
                     idx.addLayerTypeIndex(layerName, layer.layerTypeIndex);
                     refreshedLayers.add(layerName);
                  }
                  finally {
                     layerSys.releaseDynLock(false);
                  }
               }
               else {
                  acquireDynLock(false);
                  try {
                     boolean res;
                     if (curSys != null)
                        res = curSys.refreshLayerTypeIndexFile(layerName, refreshCtx, false, startTime);
                     else
                        res = this.refreshLayerTypeIndexFile(layerName, refreshCtx, false, startTime);
                     if (!res)
                        return false;
                     if (curSys == null)
                        curSys = findSystemFromTypeIndexIdent(typeIndexIdent);
                  }
                  finally {
                     releaseDynLock(false);
                  }
               }
            }
            else {
               filesToProcess.remove(indexFileName);
            }
            if (!checkIndexProgress(startTime))
               return false;
         }
         if (!rebuildLayersTypeIndex(refreshCtx.layersToRebuild, startTime)) {
            return false;
         }
         for (LayerTypeIndex startIndex:refreshCtx.toStartLaterIndexes) {
            for (BodyTypeDeclaration toStartLater:startIndex.toStartLaterTypes) {
               JavaModel toStartModel = toStartLater.getJavaModel();
               Layer toStartLayer = toStartModel.getLayer();
               if (toStartLayer.closed)
                  toStartLayer.markClosed(false, false);
               ParseUtil.initAndStartComponent(toStartModel);
            }
         }
      }
      /*
      if (!peerMode && peerSystems != null) {
         for (LayeredSystem peerSys:peerSystems) {
            peerSys.loadTypeIndex(refreshedLayers);
         }
      }
      */
      return true;
   }
private boolean loadTypeIndex ( Set < String > refreshedLayers ) { String [ ] runtimeDirNames = getRuntimeDirNames ( true ) ; if ( peerMode ) System . err . println ( "*** TypeIndexEntry should only be loaded in the main layeredSystem" ) ; long startTime = System . currentTimeMillis ( ) ; acquireDynLock ( false ) ; try { clearLeafLayerNames ( ) ; if ( typeIndexProcessMap == null ) { typeIndexProcessMap = new ConcurrentHashMap < String , SysTypeIndex > ( ) ; if ( typeIndex == null ) { typeIndex = new SysTypeIndex ( this , getTypeIndexIdent ( ) ) ; } } else return true ; } finally { releaseDynLock ( false ) ; } for ( String runtimeDirName : runtimeDirNames ) { if ( ! runtimeDirName . startsWith ( TYPE_INDEX_DIR_PREFIX ) ) continue ; String typeIndexIdent = runtimeDirName . substring ( TYPE_INDEX_DIR_PREFIX . length ( ) ) ; File typeIndexDir = new File ( getTypeIndexDir ( typeIndexIdent ) ) ; String [ ] fileNames = typeIndexDir . list ( ) ; if ( fileNames == null ) { error ( "*** Load typeIndex with no typeIndex directory: " + typeIndexDir . getPath ( ) ) ; return true ; } if ( fileNames . length == 0 ) continue ; HashMap < String , Boolean > filesToProcess = new HashMap < String , Boolean > ( ) ; for ( String file : fileNames ) { if ( file . equals ( MAIN_SYSTEM_MARKER_FILE ) ) continue ; filesToProcess . put ( file , Boolean . FALSE ) ; } LayeredSystem curSys = findSystemFromTypeIndexIdent ( typeIndexIdent ) ; SysTypeIndex curTypeIndex = typeIndexProcessMap . get ( typeIndexIdent ) ; if ( curTypeIndex == null && curSys != null && curSys . typeIndex != null ) { curTypeIndex = curSys . typeIndex ; typeIndexProcessMap . put ( typeIndexIdent , curTypeIndex ) ; } if ( curTypeIndex == null ) { typeIndexProcessMap . put ( typeIndexIdent , curTypeIndex = new SysTypeIndex ( curSys , typeIndexIdent ) ) ; } if ( curSys != null && curSys . typeIndex == null ) curSys . typeIndex = curTypeIndex ; boolean foundIndex = curTypeIndex . loadFromDir ( getTypeIndexDir ( typeIndexIdent ) ) ; if ( ! foundIndex ) { warning ( "Failed to load type index order file - rebuilding the type index" ) ; options . typeIndexMode = TypeIndexMode . Rebuild ; } RefreshTypeIndexContext refreshCtx = new RefreshTypeIndexContext ( typeIndexIdent , filesToProcess , curTypeIndex , refreshedLayers , typeIndexDir ) ; while ( filesToProcess . size ( ) > 0 ) { String indexFileName = filesToProcess . keySet ( ) . iterator ( ) . next ( ) ; if ( indexFileName . endsWith ( Layer . TYPE_INDEX_SUFFIX ) ) { String uname = indexFileName . substring ( 0 , indexFileName . length ( ) - Layer . TYPE_INDEX_SUFFIX . length ( ) ) ; String layerName = Layer . getLayerNameFromUniqueUnderscoreName ( uname ) ; Layer layer = curSys == null ? null : curSys . lookupLayerSync ( layerName , false , true ) ; if ( layer != null ) { filesToProcess . remove ( indexFileName ) ; LayeredSystem layerSys = layer . layeredSystem ; layerSys . acquireDynLock ( false ) ; try { SysTypeIndex sysIdx = layerSys . typeIndex ; LayerListTypeIndex idx = layer . activated ? sysIdx . activeTypeIndex : sysIdx . inactiveTypeIndex ; if ( writeLocked == 0 ) { System . err . println ( "*** Modifying type index without write lock" ) ; new Throwable ( ) . printStackTrace ( ) ; } if ( layer . layerTypeIndex == null ) layer . initTypeIndex ( ) ; idx . addLayerTypeIndex ( layerName , layer . layerTypeIndex ) ; refreshedLayers . add ( layerName ) ; } finally { layerSys . releaseDynLock ( false ) ; } } else { acquireDynLock ( false ) ; try { boolean res ; if ( curSys != null ) res = curSys . refreshLayerTypeIndexFile ( layerName , refreshCtx , false , startTime ) ; else res = this . refreshLayerTypeIndexFile ( layerName , refreshCtx , false , startTime ) ; if ( ! res ) return false ; if ( curSys == null ) curSys = findSystemFromTypeIndexIdent ( typeIndexIdent ) ; } finally { releaseDynLock ( false ) ; } } } else { filesToProcess . remove ( indexFileName ) ; } if ( ! checkIndexProgress ( startTime ) ) return false ; } if ( ! rebuildLayersTypeIndex ( refreshCtx . layersToRebuild , startTime ) ) { return false ; } for ( LayerTypeIndex startIndex : refreshCtx . toStartLaterIndexes ) { for ( BodyTypeDeclaration toStartLater : startIndex . toStartLaterTypes ) { JavaModel toStartModel = toStartLater . getJavaModel ( ) ; Layer toStartLayer = toStartModel . getLayer ( ) ; if ( toStartLayer . closed ) toStartLayer . markClosed ( false , false ) ; ParseUtil . initAndStartComponent ( toStartModel ) ; } } } /*
      if (!peerMode && peerSystems != null) {
         for (LayeredSystem peerSys:peerSystems) {
            peerSys.loadTypeIndex(refreshedLayers);
         }
      }
      */ return true ; }
The internal method to load the main type index.
The internal method to load the main type index .
// A simple guard to prevent this from being done twice - the first one to create the process map should init the index. // We may create types_java directories for this layered system before we've used it for a specific process // TODO: do we need to refresh this layer's type index here?   Or did it already get refreshed? // The process of getting a layer may have created this runtime so use it as soon as it becomes available.