Skip to content

Commit

Permalink
cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
David Alexandre Marques dos Santos committed May 24, 2019
1 parent 1b7318b commit 0346c4e
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 44 deletions.
12 changes: 12 additions & 0 deletions core/src/main/java/pt/webdetails/cpf/PluginEnvironment.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
/*!
* Copyright 2002 - 2019 Webdetails, a Hitachi Vantara company. All rights reserved.
*
* This software was developed by Webdetails and is provided under the terms
* of the Mozilla Public License, Version 2.0, or any later version. You may not use
* this file except in compliance with the license. If you need a copy of the license,
* please go to http://mozilla.org/MPL/2.0/. The Initial Developer is Webdetails.
*
* Software distributed under the Mozilla Public License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
* the license for the specific language governing your rights and limitations.
*/
package pt.webdetails.cpf;

import pt.webdetails.cpf.context.api.IUrlProvider;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Copyright 2002 - 2017 Webdetails, a Hitachi Vantara company. All rights reserved.
* Copyright 2002 - 2019 Webdetails, a Hitachi Vantara company. All rights reserved.
*
* This software was developed by Webdetails and is provided under the terms
* of the Mozilla Public License, Version 2.0, or any later version. You may not use
Expand All @@ -10,7 +10,6 @@
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
* the license for the specific language governing your rights and limitations.
*/

package pt.webdetails.cpf.packager;

import pt.webdetails.cpf.context.api.IUrlProvider;
Expand All @@ -29,15 +28,12 @@
import java.util.LinkedHashMap;
import java.util.Map;


/**
* A set of css|js files that can be packaged into a single file.<br> Encompasses former functionality of
* DependenciesEngine/Packager<br> will be made abstract, more specific subclasses
*/
public class DependenciesPackage {

// private static Log logger = LogFactory.getLog(DependenciesPackage.class);

public enum PackagingMode {
MINIFY, CONCATENATE
}
Expand Down Expand Up @@ -66,8 +62,8 @@ public enum PackageType {
*/
public DependenciesPackage( String name, PackageType type, IContentAccessFactory factory, IUrlProvider urlProvider ) {
this.name = name;
fileDependencies = new LinkedHashMap<String, FileDependency>();
rawDependencies = new LinkedHashMap<String, SnippetDependency>();
this.fileDependencies = new LinkedHashMap<>();
this.rawDependencies = new LinkedHashMap<>();
this.type = type;
this.factory = factory;
this.urlProvider = urlProvider;
Expand All @@ -83,28 +79,34 @@ public DependenciesPackage( String name, PackageType type, IContentAccessFactory
* @return
*/
public boolean registerFileDependency( String name, String version, PathOrigin origin, String path ) {
FileDependency newDep = new FileDependency( version, origin, path, urlProvider );
synchronized ( packagingLock ) {
if ( registerDependency( name, newDep, fileDependencies ) ) {
final FileDependency dependency = new FileDependency( version, origin, path, this.urlProvider );

synchronized ( this.packagingLock ) {
if ( registerDependency( name, dependency, this.fileDependencies ) ) {
//invalidate packaged if there
packagedDependency = null;
this.packagedDependency = null;

return true;
}
}

return false;
}

public boolean registerRawDependency( String name, String version, String contents ) {
SnippetDependency snip = new SnippetDependency( version, contents );
return registerDependency( name, snip, rawDependencies );
final SnippetDependency snip = new SnippetDependency( version, contents );

return registerDependency( name, snip, this.rawDependencies );
}

protected <T extends Dependency> boolean registerDependency( String name, T dependency, Map<String, T> registry ) {
Dependency dep = registry.get( name );
final Dependency dep = registry.get( name );
if ( dep == null || dep.isOlderVersionThan( dependency ) ) {
registry.put( name, dependency );

return true;
}

return false;
}

Expand All @@ -121,10 +123,12 @@ public String getDependencies( StringFilter format, boolean isPackaged ) {

public String getRawDependencies( boolean isPackaged ) {
StringBuilder sb = new StringBuilder();

for ( SnippetDependency dep : rawDependencies.values() ) {
sb.append( dep.getContents() );
sb.append( '\n' );
}

return sb.toString();
}

Expand Down Expand Up @@ -166,8 +170,8 @@ public String getDependencies( StringFilter format, boolean isPackaged, IDepende
}

public String getUnpackagedDependencies( StringFilter format, IDependencyInclusionFilter filter ) {
StringBuilder sb = new StringBuilder();
sb.append( "\n" );
StringBuilder sb = new StringBuilder( "\n" );

if ( filter != null ) {
// return dashboard component dependencies
for ( FileDependency dep : fileDependencies.values() ) {
Expand All @@ -181,23 +185,27 @@ public String getUnpackagedDependencies( StringFilter format, IDependencyInclusi
sb.append( format.filter( dep.getDependencyInclude() ) );
}
}

return sb.toString();
}

protected String getPackagedDependency( StringFilter format, IDependencyInclusionFilter filter ) {
boolean isMap = type.equals( PackageType.MAP );
if ( filter != null ) {
// return minified dashboard component dependencies
Map<String, FileDependency> customDependencies = new LinkedHashMap<String, FileDependency>();
Map<String, FileDependency> customDependencies = new LinkedHashMap<>();
for ( FileDependency dep : fileDependencies.values() ) {
if ( filter.include( dep ) ) {
customDependencies.put( dep.getDependencyInclude(), dep );
}
}

String packagedPath = isMap ? name : name + "." + type.toString().toLowerCase();
String baseDir = type.toString().toLowerCase();

IRWAccess writer = factory.getPluginSystemWriter( baseDir );
PathOrigin origin = new StaticSystemOrigin( baseDir );

switch ( type ) {
case CSS:
return format.filter(
Expand All @@ -219,8 +227,10 @@ protected String getPackagedDependency( StringFilter format, IDependencyInclusio
if ( packagedDependency == null ) {
String packagedPath = isMap ? name : name + "." + type.toString().toLowerCase();
String baseDir = isMap ? "css" : type.toString().toLowerCase();

IRWAccess writer = factory.getPluginSystemWriter( baseDir );
PathOrigin origin = new StaticSystemOrigin( baseDir );

switch ( type ) {
case CSS:
packagedDependency =
Expand All @@ -239,6 +249,7 @@ protected String getPackagedDependency( StringFilter format, IDependencyInclusio
getClass().getSimpleName() + " does not have a recognized type: " + type );
}
}

return format.filter( packagedDependency.getDependencyInclude() );
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Copyright 2013-2018 Webdetails, a Hitachi Vantara company. All rights reserved.
* Copyright 2013 - 2019 Webdetails, a Hitachi Vantara company. All rights reserved.
*
* This software was developed by Webdetails and is provided under the terms
* of the Mozilla Public License, Version 2.0, or any later version. You may not use
Expand All @@ -10,7 +10,6 @@
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
* the license for the specific language governing your rights and limitations.
*/

package pt.webdetails.cpf.packager.dependencies;

import org.apache.commons.io.IOUtils;
Expand All @@ -36,12 +35,14 @@ public class FileDependency extends Dependency {
protected PathOrigin origin;
protected IUrlProvider urlProvider;
private String hash;

// TODO: why not just a timestamp?
// use checksums for versions, otherwise use timestamps
protected boolean useChecksumVersion = true;

public FileDependency( String version, PathOrigin origin, String path, IUrlProvider urlProvider ) {
super();

this.filePath = path;
this.hash = null;
this.origin = origin;
Expand All @@ -60,6 +61,7 @@ protected String getCheckSum() {
IOUtils.closeQuietly( in );
}
}

return hash;
}

Expand All @@ -72,12 +74,16 @@ protected long getTimeStamp() {
}

public InputStream getFileInputStream() throws IOException {
if ( getContentFactory() != null ) {
return origin.getReader( getContentFactory() ).getFileInputStream( filePath );
} else {
logger.fatal( String.format( "Couldn't getFileInputStream() for filePath = '%s'. Unable to get ContentFactory.", filePath ) );
return null;
final IContentAccessFactory contentFactory = getContentFactory();
if ( contentFactory != null ) {
return this.origin.getReader( contentFactory ).getFileInputStream( this.filePath );
}

logger.fatal(
String.format( "Couldn't getFileInputStream() for filePath = '%s'. Unable to get ContentFactory.", this.filePath )
);

return null;
}

/**
Expand All @@ -87,7 +93,8 @@ public String getDependencyInclude() {
// the ?v=<version> is used to bypass browser cache when needed
String version = getVersion();
String urlAppend = ( ( version == null ) ? "" : "?v=" + version );
return origin.getUrl( filePath, urlProvider ) + urlAppend;

return getUrlFilePath() + urlAppend;
}

@Override
Expand All @@ -100,14 +107,15 @@ public String getUrlFilePath() {
}

protected IContentAccessFactory getContentFactory() {
if ( PluginEnvironment.env() != null ) {
return PluginEnvironment.env().getContentAccessFactory();
} else {
return null;
final PluginEnvironment environment = PluginEnvironment.env();
if ( environment != null ) {
return environment.getContentAccessFactory();
}

return null;
}

public String toString() {
return filePath;
return this.filePath;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Copyright 2002 - 2017 Webdetails, a Hitachi Vantara company. All rights reserved.
* Copyright 2002 - 2019 Webdetails, a Hitachi Vantara company. All rights reserved.
*
* This software was developed by Webdetails and is provided under the terms
* of the Mozilla Public License, Version 2.0, or any later version. You may not use
Expand All @@ -10,7 +10,6 @@
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
* the license for the specific language governing your rights and limitations.
*/

package pt.webdetails.cpf;

import java.io.IOException;
Expand Down Expand Up @@ -75,20 +74,26 @@ public String getPluginId() {
if ( pluginId == null ) {
try {
// this depends on cpf being loaded by the plugin classloader
IReadAccess reader =
new SystemPluginResourceAccess( PentahoBasePluginEnvironment.class.getClassLoader(), null );
Node documentNode = XmlDom4JUtils.getDocumentFromFile( reader, "plugin.xml" ).getRootElement();
pluginId = documentNode.valueOf( "/plugin/@name" );
if ( StringUtils.isEmpty( pluginId ) ) {
pluginId = documentNode.valueOf( "/plugin/@title" );
}
final Node documentNode = getPluginXmlRootElement();

final String name = documentNode.valueOf( "/plugin/@name" );
final String title = documentNode.valueOf( "/plugin/@title" );

pluginId = StringUtils.isEmpty( name ) ? title : name;
} catch ( IOException e ) {
logger.fatal( "Problem reading plugin.xml", e );

return "cpf";
}
}

return pluginId;
}

private Node getPluginXmlRootElement() throws IOException {
final ClassLoader pluginClassloader = PentahoBasePluginEnvironment.class.getClassLoader();
final IReadAccess reader = new SystemPluginResourceAccess( pluginClassloader, null );

return XmlDom4JUtils.getDocumentFromFile( reader, "plugin.xml" ).getRootElement();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Copyright 2002 - 2017 Webdetails, a Hitachi Vantara company. All rights reserved.
* Copyright 2002 - 2019 Webdetails, a Hitachi Vantara company. All rights reserved.
*
* This software was developed by Webdetails and is provided under the terms
* of the Mozilla Public License, Version 2.0, or any later version. You may not use
Expand All @@ -10,7 +10,6 @@
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
* the license for the specific language governing your rights and limitations.
*/

package pt.webdetails.cpf;

import org.apache.commons.logging.Log;
Expand All @@ -22,7 +21,6 @@

public abstract class SimpleLifeCycleListener implements IPluginLifecycleListener, IPlatformReadyListener {


static Log logger = LogFactory.getLog( SimpleLifeCycleListener.class );

@Override
Expand All @@ -37,12 +35,16 @@ public void loaded() throws PluginLifecycleException {
@Override
public void unLoaded() throws PluginLifecycleException {
logger.debug( "Shutting down and shutting down Orient DB " );

PersistenceEngine.shutdown();
}

@Override
public void ready() throws PluginLifecycleException {
if ( CpfProperties.getInstance().getBooleanProperty( "USE_PERSISTENCE", false ) ) {
public void ready() {
final CpfProperties cpfProperties = CpfProperties.getInstance();

final boolean usePersistence = cpfProperties.getBooleanProperty( "USE_PERSISTENCE", false );
if ( usePersistence ) {
PersistenceEngine.getInstance();
}
}
Expand Down

0 comments on commit 0346c4e

Please sign in to comment.