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.HashSet;
  import java.util.Map;
  import java.util.Set;
 
 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.backend.file.AntPathMatcher;
 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 );
 
     private static final AntPathMatcher ANT_PATH_MATCHER = new AntPathMatcher();
 
     @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();
 
         //Get a "white list" of package names that are available for authoring
         final Set<StringpackageNamesWhiteList = loadPackageNameWhiteListproject,
                                                                            kieModuleMetaData.getPackages() );
        // Add all packages that are available for authoring
        pdBuilder.addPackagespackageNamesWhiteList );
        //Add all classes from the KieModule metaData
        final Map<StringFactBuilderdiscoveredFieldFactBuilders = new HashMap<StringFactBuilder>();
        for ( final String packageName : kieModuleMetaData.getPackages() ) {
            if ( packageNamesWhiteList.containspackageName ) ) {
                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();
    }
    private Set<StringloadPackageNameWhiteListfinal KieProject project,
                                                  final Collection<StringpackageNames ) {
        final Set<StringpackageNamesWhiteList = new HashSet<String>();
        if ( packageNames == null ) {
            return packageNamesWhiteList;
        }
        packageNamesWhiteList.addAllpackageNames );
        final org.uberfire.java.nio.file.Path packageNamesWhiteListPath = Paths.convert( project.getPackageNamesWhiteList() );
        if ( Files.exists( packageNamesWhiteListPath ) ) {
            final String content = .readAllString( packageNamesWhiteListPath );
            if ( !( content == null || content.trim().isEmpty() ) ) {
                //If a White List is defined build set of acceptable Package Names from it
                packageNamesWhiteList.clear();
                final String[] patterns = content.split( System.getProperty"line.separator" ) );
                //Convert to Paths as we're delegating to an Ant-style pattern matcher.
                //Convert once outside of the nested loops for performance reasons.
                for ( int i = 0; i < patterns.lengthi++ ) {
                    patternsi ] = patternsi ].replaceAll"\\.",
                                                              AntPathMatcher.DEFAULT_PATH_SEPARATOR );
                }
                final HashMap<StringStringpackageNamePaths = new HashMap<StringString>();
                for ( String packageName : packageNames ) {
                    packageNamePaths.putpackageName,
                                          packageName.replaceAll"\\.",
                                                                  AntPathMatcher.DEFAULT_PATH_SEPARATOR ) );
                }
                //Add Package Names matching the White List to the available packages
                for ( String pattern : patterns ) {
                    for ( Map.Entry<StringStringpnp : packageNamePaths.entrySet() ) {
                        if ( .match( pattern,
                                                     pnp.getValue() ) ) {
                            packageNamesWhiteList.addpnp.getKey() );
                        }
                    }
                }
            }
        }
        return packageNamesWhiteList;
    }
New to GrepCode? Check out our FAQ X