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.List;
  import javax.inject.Named;
 
 import  org.drools.core.rule.TypeMetaInfo;
 import  org.drools.workbench.models.commons.shared.imports.Import;
 import  org.drools.workbench.models.commons.shared.imports.Imports;
 import  org.guvnor.common.services.backend.cache.LRUCache;
 import  org.guvnor.common.services.builder.Builder;
 import  org.guvnor.common.services.builder.LRUBuilderCache;
 import  org.guvnor.common.services.project.builder.events.InvalidateDMOProjectCacheEvent;
 import  org.guvnor.common.services.project.builder.model.TypeSource;
 import  org.guvnor.common.services.project.model.Project;
 import  org.guvnor.common.services.project.model.ProjectImports;
 import  org.guvnor.common.services.project.service.POMService;
 import  org.guvnor.common.services.project.service.ProjectService;
 import  org.kie.api.definition.KiePackage;
 import  org.kie.api.definition.rule.Rule;
 import  org.kie.api.runtime.KieContainer;
 import  org.kie.commons.io.IOService;
 import  org.kie.commons.java.nio.file.Files;
 import  org.kie.commons.validation.PortablePreconditions;
 import  org.kie.scanner.KieModuleMetaData;
 import  org.kie.workbench.common.services.datamodel.oracle.ProjectDataModelOracle;
 import  org.uberfire.backend.server.util.Paths;
 import  org.uberfire.backend.vfs.Path;

A simple LRU cache for Project DataModelOracles
 
 @Named("ProjectDataModelOracleCache")
 public class LRUProjectDataModelOracleCache extends LRUCache<Project, ProjectDataModelOracle> {
 
     private static final Logger log = LoggerFactory.getLoggerLRUProjectDataModelOracleCache.class );
 
     @Inject
     private Paths paths;
 
     @Inject
     private POMService pomService;
 
     @Inject
     @Named("ioStrategy")
     private IOService ioService;
 
     @Inject
     private ProjectService projectService;
 
     @Inject
     private LRUBuilderCache cache;
 
     public synchronized void invalidateProjectCache( @Observes final InvalidateDMOProjectCacheEvent event ) {
         PortablePreconditions.checkNotNull( "event",
                                             event );
         final Path resourcePath = event.getResourcePath();
         final Project 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 Project project ) {
         ProjectDataModelOracle projectOracle = getEntry( project );
         if ( projectOracle == null ) {
             projectOracle = makeProjectOracleproject );
             setEntry( project,
                       projectOracle );
         }
         return projectOracle;
     }
 
     private ProjectDataModelOracle makeProjectOraclefinal Project 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
         // XXX: For Testing
         ArrayList<Stringtemp = new ArrayList<String>();
         temp.add("A.B.C");
         temp.add("B.C.D");
         temp.add("C.D.E");
         pdBuilder.addPackages(temp);
//        pdBuilder.addPackages(kieModuleMetaData.getPackages());
        //Add all classes from the KieModule metaData
        for ( final String packageName : kieModuleMetaData.getPackages() ) {
            for ( final String className : kieModuleMetaData.getClasses( packageName ) ) {
                final Class clazz = kieModuleMetaData.getClass( packageName,
                                                       className );
                final TypeMetaInfo typeMetaInfo = kieModuleMetaData.getTypeMetaInfo( clazz );
                final TypeSource typeSource = builder.getClassSource( kieModuleMetaData,
                                                                      clazz );
                try {
                    pdBuilder.addClassclazz,
                                        typeMetaInfo.isEvent(),
                                        typeSource );
                } catch ( IOException ioe ) {
                    .errorioe.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.kie.commons.java.nio.file.Path nioExternalImportsPath = .convert( project.getImportsPath() );
        if ( Files.exists( nioExternalImportsPath ) ) {
            final Path externalImportsPath = .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 );
                } catch ( ClassNotFoundException cnfe ) {
                    //This should not happen as Builder would have failed to load them and failed fast.
                    .errorcnfe.getMessage() );
                } catch ( IOException ioe ) {
                    .errorioe.getMessage() );
                }
            }
        }
        addAllRuleNamesbuilderpdBuilder );
        return pdBuilder.build();
    }
    private void addAllRuleNames( Builder builder,
                                  ProjectDataModelOracleBuilder pdBuilder ) {
        final KieModuleMetaData kieModuleMetaData = KieModuleMetaData.Factory.newKieModuleMetaData(builder.getKieModuleIgnoringErrors());
        final List<StringruleNames = new ArrayList<String>();
        for (String packageName : kieModuleMetaData.getPackages()) {
            ruleNames.addAll(kieModuleMetaData.getRuleNames(packageName));
        }
        pdBuilder.addRuleNamesruleNames );
    }
New to GrepCode? Check out our FAQ X