Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.kie.workbench.common.services.datamodel.backend.server.cache;
  
  import java.util.HashMap;
  import java.util.Map;
  import javax.inject.Named;
 
 import  org.drools.core.rule.TypeMetaInfo;
 import  org.drools.workbench.models.datamodel.imports.Import;
 import  org.drools.workbench.models.datamodel.imports.Imports;
 import  org.drools.workbench.models.datamodel.oracle.ProjectDataModelOracle;
 import  org.drools.workbench.models.datamodel.oracle.TypeSource;
 import  org.guvnor.common.services.backend.cache.LRUCache;
 import  org.guvnor.common.services.project.builder.events.InvalidateDMOProjectCacheEvent;
 import  org.guvnor.common.services.project.model.ProjectImports;
 import  org.guvnor.common.services.project.service.POMService;
 import  org.kie.scanner.KieModuleMetaData;
 import  org.kie.workbench.common.services.backend.builder.Builder;
 import  org.kie.workbench.common.services.backend.builder.LRUBuilderCache;
 import  org.kie.workbench.common.services.shared.project.KieProject;
 import  org.kie.workbench.common.services.shared.project.KieProjectService;
 import  org.kie.workbench.common.services.shared.project.ProjectImportsService;
 import  org.uberfire.backend.server.util.Paths;
 import  org.uberfire.backend.vfs.Path;
 import  org.uberfire.commons.validation.PortablePreconditions;
 import  org.uberfire.io.IOService;
 import  org.uberfire.java.nio.file.Files;

A simple LRU cache for Project DataModelOracles
 
 @Named("ProjectDataModelOracleCache")
 public class LRUProjectDataModelOracleCache extends LRUCache<KieProject, ProjectDataModelOracle> {
 
     private static final Logger log = LoggerFactory.getLoggerLRUProjectDataModelOracleCache.class );
 
     @Inject
     private POMService pomService;
 
     @Inject
     @Named("ioStrategy")
     private IOService ioService;
 
     @Inject
     private KieProjectService projectService;
 
     @Inject
     private ProjectImportsService importsService;
 
     @Inject
     private LRUBuilderCache cache;
 
     public synchronized void invalidateProjectCache( @Observes final InvalidateDMOProjectCacheEvent event ) {
         PortablePreconditions.checkNotNull( "event",
                                             event );
         final Path resourcePath = event.getResourcePath();
         final KieProject project = .resolveProject( resourcePath );
 
         //If resource was not within a Project there's nothing to invalidate
         if ( project != null ) {
             invalidateCache( project );
         }
     }
 
     //Check the ProjectOracle for the Project has been created, otherwise create one!
     public synchronized ProjectDataModelOracle assertProjectDataModelOraclefinal KieProject project ) {
         ProjectDataModelOracle projectOracle = getEntry( project );
         if ( projectOracle == null ) {
             projectOracle = makeProjectOracleproject );
             setEntry( project,
                       projectOracle );
         }
         return projectOracle;
     }
 
     private ProjectDataModelOracle makeProjectOraclefinal KieProject project ) {
         //Get a Builder for the project
         final Builder builder = .assertBuilder( project );
 
         //Create the ProjectOracle...
         final KieModuleMetaData kieModuleMetaData = KieModuleMetaData.Factory.newKieModuleMetaData( builder.getKieModuleIgnoringErrors() );
         final ProjectDataModelOracleBuilder pdBuilder = ProjectDataModelOracleBuilder.newProjectOracleBuilder();
 
         // Add all packages
         pdBuilder.addPackageskieModuleMetaData.getPackages() );
 
         //Add all classes from the KieModule metaData
         final Map<StringFactBuilderdiscoveredFieldFactBuilders = new HashMap<StringFactBuilder>(  );
         for ( final String packageName : kieModuleMetaData.getPackages() ) {
             for ( final String className : kieModuleMetaData.getClasses( packageName ) ) {
                 try {
                    final Class clazz = kieModuleMetaData.getClass( packageName,
                                                                    className );
                    final TypeMetaInfo typeMetaInfo = kieModuleMetaData.getTypeMetaInfo( clazz );
                    final TypeSource typeSource = builder.getClassSource( kieModuleMetaData,
                                                                          clazz );
                    pdBuilder.addClassclazz,
                                        discoveredFieldFactBuilders,
                                        typeMetaInfo.isEvent(),
                                        typeSource );
                } catch ( Throwable e ) {
                    .errore.getMessage() );
                }
            }
        }
        //Add external imports. The availability of these classes is checked in Builder and failed fast. Here we load them into the DMO
        final org.uberfire.java.nio.file.Path nioExternalImportsPath = Paths.convert( project.getImportsPath() );
        if ( Files.exists( nioExternalImportsPath ) ) {
            final Path externalImportsPath = Paths.convert( nioExternalImportsPath );
            final ProjectImports projectImports = .load( externalImportsPath );
            final Imports imports = projectImports.getImports();
            for ( final Import item : imports.getImports() ) {
                try {
                    Class clazz = this.getClass().getClassLoader().loadClassitem.getType() );
                    pdBuilder.addClassclazz,
                                        discoveredFieldFactBuilders,
                                        false,
                                        TypeSource.JAVA_DEPENDENCY );
                } catch ( ClassNotFoundException cnfe ) {
                    //This would have been raised to the user by Builder's validation but record the error here too
                    .errorcnfe.getMessage() );
                } catch ( IOException ioe ) {
                    .errorioe.getMessage() );
                }
            }
        }
        return pdBuilder.build();
    }
New to GrepCode? Check out our FAQ X