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.enterprise.context.ApplicationScoped;
  import  javax.enterprise.event.Observes;
  import  javax.inject.Inject;
  import  javax.inject.Named;
 
 import  org.drools.workbench.models.datamodel.oracle.PackageDataModelOracle;
 import  org.drools.workbench.models.datamodel.oracle.ProjectDataModelOracle;
 import  org.guvnor.common.services.backend.cache.LRUCache;
 import  org.guvnor.common.services.backend.file.FileDiscoveryService;
 import  org.guvnor.common.services.project.builder.events.InvalidateDMOPackageCacheEvent;
 import  org.guvnor.common.services.project.builder.events.InvalidateDMOProjectCacheEvent;
 import  org.guvnor.common.services.project.builder.model.BuildMessage;
 import  org.guvnor.common.services.project.model.Package;
 import  org.guvnor.common.services.project.model.Project;
 import  org.guvnor.common.services.project.service.ProjectService;
 import  org.uberfire.io.IOService;
 import  org.uberfire.java.nio.file.DirectoryStream;
 import  org.uberfire.commons.validation.PortablePreconditions;
 import  org.kie.workbench.common.services.backend.file.DslFileFilter;
 import  org.kie.workbench.common.services.backend.file.EnumerationsFileFilter;
 import  org.kie.workbench.common.services.backend.file.GlobalsFileFilter;
 import  org.uberfire.backend.server.util.Paths;
 import  org.uberfire.backend.vfs.Path;

A simple LRU cache for Package DataModelOracles
 
 @ApplicationScoped
 @Named("PackageDataModelOracleCache")
 public class LRUDataModelOracleCache extends LRUCache<Package, PackageDataModelOracle> {
 
     private static final DirectoryStream.Filter<org.uberfire.java.nio.file.Path> FILTER_ENUMERATIONS = new EnumerationsFileFilter();
 
     private static final DirectoryStream.Filter<org.uberfire.java.nio.file.Path> FILTER_GLOBALS = new GlobalsFileFilter();
 
     private static final DirectoryStream.Filter<org.uberfire.java.nio.file.Path> FILTER_DSLS = new DslFileFilter();
 
     @Inject
     @Named("ioStrategy")
     private IOService ioService;
 
     @Inject
     private FileDiscoveryService fileDiscoveryService;
 
     @Inject
     @Named("ProjectDataModelOracleCache")
 
     @Inject
     private ProjectService projectService;
 
     public synchronized void invalidatePackageCache( @Observes final InvalidateDMOPackageCacheEvent event ) {
         PortablePreconditions.checkNotNull( "event",
                                             event );
         final Path resourcePath = event.getResourcePath();
         final Package pkg = .resolvePackage( resourcePath );
 
         //If resource was not within a Package there's nothing to invalidate
         if ( pkg != null ) {
             invalidateCachepkg );
         }
     }
 
     public synchronized void invalidateProjectPackagesCache( @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 ) {
             return;
         }
 
         final String projectUri = project.getRootPath().toURI();
         final List<PackagecacheEntriesToInvalidate = new ArrayList<Package>();
         for ( final Package pkg : getKeys() ) {
             final Path packageMainSrcPath = pkg.getPackageMainSrcPath();
             final Path packageTestSrcPath = pkg.getPackageTestSrcPath();
             final Path packageMainResourcesPath = pkg.getPackageMainResourcesPath();
             final Path packageTestResourcesPath = pkg.getPackageTestResourcesPath();
             if ( packageMainSrcPath != null && packageMainSrcPath.toURI().startsWith( projectUri ) ) {
                 cacheEntriesToInvalidate.addpkg );
             } else if ( packageTestSrcPath != null && packageTestSrcPath.toURI().startsWith( projectUri ) ) {
                 cacheEntriesToInvalidate.addpkg );
             } else if ( packageMainResourcesPath != null && packageMainResourcesPath.toURI().startsWith( projectUri ) ) {
                 cacheEntriesToInvalidate.addpkg );
             } else if ( packageTestResourcesPath != null && packageTestResourcesPath.toURI().startsWith( projectUri ) ) {
                 cacheEntriesToInvalidate.addpkg );
             }
         }
         for ( final Package pkg : cacheEntriesToInvalidate ) {
             invalidateCachepkg );
        }
    }
    //Check the DataModelOracle for the Package has been created, otherwise create one!
    public synchronized PackageDataModelOracle assertPackageDataModelOraclefinal Project project,
                                                                             final Package pkg ) {
        PackageDataModelOracle oracle = getEntrypkg );
        if ( oracle == null ) {
            oracle = makePackageDataModelOracleproject,
                                                 pkg );
            setEntry( pkg,
                      oracle );
        }
        return oracle;
    }
    private PackageDataModelOracle makePackageDataModelOraclefinal Project project,
                                                               final Package pkg ) {
        final String packageName = pkg.getPackageName();
        final PackageDataModelOracleBuilder dmoBuilder = PackageDataModelOracleBuilder.newPackageOracleBuilderpackageName );
        final ProjectDataModelOracle projectOracle = .assertProjectDataModelOracleproject );
        dmoBuilder.setProjectOracleprojectOracle );
        //Add Guvnor enumerations
        loadEnumsForPackagedmoBuilder,
                             pkg );
        //Add DSLs
        loadDslsForPackagedmoBuilder,
                            pkg );
        //Add Globals
        loadGlobalsForPackagedmoBuilder,
                               pkg );
        return dmoBuilder.build();
    }
    private BuildMessage makeMessagefinal String msg ) {
        final BuildMessage buildMessage = new BuildMessage();
        buildMessage.setLevel( BuildMessage.Level.ERROR );
        buildMessage.setText( msg );
        return buildMessage;
    }
    private void loadEnumsForPackagefinal PackageDataModelOracleBuilder dmoBuilder,
                                      final Package pkg ) {
        final org.uberfire.java.nio.file.Path nioPackagePath = Paths.convert( pkg.getPackageMainResourcesPath() );
        final Collection<org.uberfire.java.nio.file.Path> enumFiles = .discoverFiles( nioPackagePath,
                                                                                                             FILTER_ENUMERATIONS );
        for ( final org.uberfire.java.nio.file.Path path : enumFiles ) {
            final String enumDefinition = .readAllString( path );
            dmoBuilder.addEnumenumDefinition );
        }
    }
    private void loadDslsForPackagefinal PackageDataModelOracleBuilder dmoBuilder,
                                     final Package pkg ) {
        final org.uberfire.java.nio.file.Path nioPackagePath = Paths.convert( pkg.getPackageMainResourcesPath() );
        final Collection<org.uberfire.java.nio.file.Path> dslFiles = .discoverFiles( nioPackagePath,
                                                                                                            FILTER_DSLS );
        for ( final org.uberfire.java.nio.file.Path path : dslFiles ) {
            final String dslDefinition = .readAllString( path );
            dmoBuilder.addDsldslDefinition );
        }
    }
    private void loadGlobalsForPackagefinal PackageDataModelOracleBuilder dmoBuilder,
                                        final Package pkg ) {
        final org.uberfire.java.nio.file.Path nioPackagePath = Paths.convert( pkg.getPackageMainResourcesPath() );
        final Collection<org.uberfire.java.nio.file.Path> globalFiles = .discoverFiles( nioPackagePath,
                                                                                                               FILTER_GLOBALS );
        for ( final org.uberfire.java.nio.file.Path path : globalFiles ) {
            final String definition = .readAllString( path );
            dmoBuilder.addGlobalsdefinition );
        }
    }
New to GrepCode? Check out our FAQ X