From b7aea3e0995a52e4bf88f9f66797b41d25384350 Mon Sep 17 00:00:00 2001 From: Duy Date: Fri, 15 Jun 2018 19:25:10 +0700 Subject: [PATCH] remove common io dependencies, it cause crash because using new api --- app/build.gradle | 9 +- app/release/output.json | 2 +- .../AndroidLibraryProjectExtractorTest.java | 10 +- .../java/com/duy/common/purchase/Premium.java | 2 +- .../duy/common/purchase/PremiumFileUtil.java | 3 +- .../com/duy/ide/javaide/JavaIdeActivity.java | 4 + .../autocomplete/parser/JavaClassManager.java | 3 +- lib-android-compiler/build.gradle | 4 +- .../com/duy/android/compiler/env/Assets.java | 2 +- .../compiler/project/AndroidAppProject.java | 2 +- .../project/AndroidProjectManager.java | 4 +- .../android/compiler/project/ClassUtil.java | 4 +- .../repo/maven/ArtifactDownloader.java | 272 ------------------ .../repo/maven/LocalArtifactFactory.java | 121 -------- .../repo/maven/LocalArtifactResolver.java | 6 - .../repo/maven/MojoExecutionException.java | 7 - .../repo/maven/MojoFailureException.java | 7 - .../compiler/repo/maven/SignatureUtils.java | 66 ----- .../repo/maven/cache/DownloadCache.java | 121 -------- .../repo/maven/cache/FileBackedIndex.java | 128 --------- .../compiler/repo/maven/cache/FileIndex.java | 37 --- 21 files changed, 25 insertions(+), 789 deletions(-) delete mode 100644 lib-android-compiler/src/main/java/com/duy/android/compiler/repo/maven/ArtifactDownloader.java delete mode 100644 lib-android-compiler/src/main/java/com/duy/android/compiler/repo/maven/LocalArtifactFactory.java delete mode 100644 lib-android-compiler/src/main/java/com/duy/android/compiler/repo/maven/LocalArtifactResolver.java delete mode 100644 lib-android-compiler/src/main/java/com/duy/android/compiler/repo/maven/MojoExecutionException.java delete mode 100644 lib-android-compiler/src/main/java/com/duy/android/compiler/repo/maven/MojoFailureException.java delete mode 100644 lib-android-compiler/src/main/java/com/duy/android/compiler/repo/maven/SignatureUtils.java delete mode 100644 lib-android-compiler/src/main/java/com/duy/android/compiler/repo/maven/cache/DownloadCache.java delete mode 100644 lib-android-compiler/src/main/java/com/duy/android/compiler/repo/maven/cache/FileBackedIndex.java delete mode 100644 lib-android-compiler/src/main/java/com/duy/android/compiler/repo/maven/cache/FileIndex.java diff --git a/app/build.gradle b/app/build.gradle index 7a0a7cfba..10ea19c4d 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -11,8 +11,8 @@ android { minSdkVersion rootProject.ext.minSdkVersion targetSdkVersion rootProject.ext.targetSdkVersion multiDexEnabled true - versionCode 44 - versionName "1.4.4" + versionCode 45 + versionName "1.4.5" vectorDrawables.useSupportLibrary = true testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } @@ -113,7 +113,10 @@ dependencies { api 'com.yqritc:recyclerview-flexibledivider:1.2.9' api 'com.sothree.slidinguppanel:library:3.4.0' //noinspection GradleDependency newer version cause crash -// api 'commons-io:commons-io:2.2' + api ('commons-io:commons-io:2.3'){ + transitive = true + } + api 'com.simplecityapps:recyclerview-fastscroll:1.0.18' api 'org.apache.commons:commons-lang3:3.7' api 'com.google.guava:guava:25.1-android' diff --git a/app/release/output.json b/app/release/output.json index e1c7f6b3c..eb0e47797 100644 --- a/app/release/output.json +++ b/app/release/output.json @@ -1 +1 @@ -[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":43,"versionName":"1.4.3","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}] \ No newline at end of file +[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":45,"versionName":"1.4.5","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}] \ No newline at end of file diff --git a/app/src/androidTest/java/com/duy/android/compiler/io/AndroidLibraryProjectExtractorTest.java b/app/src/androidTest/java/com/duy/android/compiler/io/AndroidLibraryProjectExtractorTest.java index 93d412d1e..371bb0d74 100644 --- a/app/src/androidTest/java/com/duy/android/compiler/io/AndroidLibraryProjectExtractorTest.java +++ b/app/src/androidTest/java/com/duy/android/compiler/io/AndroidLibraryProjectExtractorTest.java @@ -10,11 +10,8 @@ import com.duy.android.compiler.library.LibraryCache; import com.duy.android.compiler.project.AndroidAppProject; import com.duy.android.compiler.project.AndroidProjectManager; -import com.duy.android.compiler.repo.maven.ArtifactDownloader; -import com.duy.android.compiler.repo.maven.MojoExecutionException; -import com.duy.android.compiler.repo.maven.MojoFailureException; +import com.duy.common.io.IOUtils; -import org.apache.commons.io.IOUtils; import org.apache.maven.model.Model; import org.apache.maven.model.io.xpp3.MavenXpp3Reader; import org.apache.maven.project.MavenProject; @@ -90,7 +87,7 @@ public void testReadPom() throws IOException { } @Test - public void testDownload() throws IOException, MojoFailureException, MojoExecutionException { + public void testDownload() throws IOException { Context context = InstrumentationRegistry.getTargetContext(); File pomFile = new File(Environment.getSdkAppDir(), "appcompat-v7-27.1.1.pom"); @@ -109,8 +106,5 @@ public void testDownload() throws IOException, MojoFailureException, MojoExecuti } MavenProject mavenProject = new MavenProject(model); System.out.println("mavenProject = " + mavenProject); - - ArtifactDownloader artifactDownloader = new ArtifactDownloader(context, mavenProject); - artifactDownloader.execute(); } } diff --git a/app/src/main/java/com/duy/common/purchase/Premium.java b/app/src/main/java/com/duy/common/purchase/Premium.java index 12ab0f7ab..b450ca06d 100644 --- a/app/src/main/java/com/duy/common/purchase/Premium.java +++ b/app/src/main/java/com/duy/common/purchase/Premium.java @@ -48,7 +48,7 @@ public class Premium { * @param context - Android context */ public static boolean isPremiumUser(Context context) { - return IS_PREMIUM || PremiumFileUtil.licenseCached(context) || true; + return IS_PREMIUM || PremiumFileUtil.licenseCached(context); } /** diff --git a/app/src/main/java/com/duy/common/purchase/PremiumFileUtil.java b/app/src/main/java/com/duy/common/purchase/PremiumFileUtil.java index 71b90f79d..48584d643 100644 --- a/app/src/main/java/com/duy/common/purchase/PremiumFileUtil.java +++ b/app/src/main/java/com/duy/common/purchase/PremiumFileUtil.java @@ -21,8 +21,7 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; - -import org.apache.commons.io.IOUtils; +import com.duy.common.io.IOUtils; import java.io.File; import java.io.FileInputStream; diff --git a/app/src/main/java/com/duy/ide/javaide/JavaIdeActivity.java b/app/src/main/java/com/duy/ide/javaide/JavaIdeActivity.java index 5bcf230bd..55bc9db2c 100644 --- a/app/src/main/java/com/duy/ide/javaide/JavaIdeActivity.java +++ b/app/src/main/java/com/duy/ide/javaide/JavaIdeActivity.java @@ -41,6 +41,7 @@ import com.duy.android.compiler.project.JavaProjectManager; import com.duy.android.compiler.utils.ProjectUtils; import com.duy.common.purchase.InAppPurchaseHelper; +import com.duy.common.purchase.Premium; import com.duy.ide.R; import com.duy.ide.code.api.CodeFormatProvider; import com.duy.ide.code.api.SuggestionProvider; @@ -171,6 +172,9 @@ public boolean onCreateOptionsMenu(Menu container) { @Override protected void onCreateNavigationMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_nav_javaide, menu); + if (Premium.isPremiumUser(this)) { + menu.findItem(R.id.action_premium).setVisible(false); + } super.onCreateNavigationMenu(menu); } diff --git a/app/src/main/java/com/duy/ide/javaide/editor/autocomplete/parser/JavaClassManager.java b/app/src/main/java/com/duy/ide/javaide/editor/autocomplete/parser/JavaClassManager.java index f2f4b8a1b..16668fbcb 100644 --- a/app/src/main/java/com/duy/ide/javaide/editor/autocomplete/parser/JavaClassManager.java +++ b/app/src/main/java/com/duy/ide/javaide/editor/autocomplete/parser/JavaClassManager.java @@ -23,6 +23,7 @@ import com.duy.android.compiler.project.JavaProject; import com.duy.common.data.Pair; import com.duy.common.interfaces.Filter; +import com.duy.common.io.IOUtils; import com.duy.ide.javaide.editor.autocomplete.model.ClassDescription; import com.duy.ide.javaide.utils.DLog; import com.sun.tools.javac.tree.JCTree; @@ -106,7 +107,7 @@ public void loadFromProject(JavaProject project) { true); for (File javaFile : javaFiles) { if (DLog.DEBUG) DLog.d(TAG, "loadFromProject: parsing class " + javaFile); - String content = com.duy.android.compiler.utils.IOUtils.toString(javaFile); + String content = IOUtils.toString(javaFile); JCTree.JCCompilationUnit ast = parser.parse(content); List parseClasses = parser.parseClasses(ast); for (IClass aClass : parseClasses) { diff --git a/lib-android-compiler/build.gradle b/lib-android-compiler/build.gradle index 68704dd97..c3d89f630 100644 --- a/lib-android-compiler/build.gradle +++ b/lib-android-compiler/build.gradle @@ -28,7 +28,6 @@ android { exclude 'META-INF/NOTICE.txt' exclude 'META-INF/notice.txt' exclude 'META-INF/ASL2.0' - exclude 'META-INF/plexus/components.xml' exclude 'META-INF/INDEX.LIST' } } @@ -54,9 +53,8 @@ dependencies { api 'com.google.guava:guava:25.1-android' //noinspection GradleDependency newer version cause crash - //api 'commons-io:commons-io:2.2' + api 'commons-io:commons-io:2.3' api 'commons-codec:commons-codec:1.10' api 'org.apache.maven:maven-project:2.2.1' - api 'org.codehaus.plexus:plexus-archiver:3.6.0' api 'com.google.code.gson:gson:2.8.1' } \ No newline at end of file diff --git a/lib-android-compiler/src/main/java/com/duy/android/compiler/env/Assets.java b/lib-android-compiler/src/main/java/com/duy/android/compiler/env/Assets.java index 8c863cf5e..03aefbd19 100644 --- a/lib-android-compiler/src/main/java/com/duy/android/compiler/env/Assets.java +++ b/lib-android-compiler/src/main/java/com/duy/android/compiler/env/Assets.java @@ -4,7 +4,7 @@ import android.content.res.AssetManager; import android.util.Log; -import org.apache.commons.io.IOUtils; +import com.duy.common.io.IOUtils; import java.io.File; import java.io.FileOutputStream; diff --git a/lib-android-compiler/src/main/java/com/duy/android/compiler/project/AndroidAppProject.java b/lib-android-compiler/src/main/java/com/duy/android/compiler/project/AndroidAppProject.java index 0b7d8e8d6..1897f1246 100644 --- a/lib-android-compiler/src/main/java/com/duy/android/compiler/project/AndroidAppProject.java +++ b/lib-android-compiler/src/main/java/com/duy/android/compiler/project/AndroidAppProject.java @@ -218,7 +218,7 @@ private void writeToLibraryFile() throws JSONException, IOException { array.put(item); } String str = jsonObject.toString(1); - org.apache.commons.io.IOUtils.write(str, new FileOutputStream(file), "UTF-8"); + IOUtils.write(str, new FileOutputStream(file), "UTF-8"); } public String getPackageForR() { diff --git a/lib-android-compiler/src/main/java/com/duy/android/compiler/project/AndroidProjectManager.java b/lib-android-compiler/src/main/java/com/duy/android/compiler/project/AndroidProjectManager.java index bfa629012..f9f3a0acd 100644 --- a/lib-android-compiler/src/main/java/com/duy/android/compiler/project/AndroidProjectManager.java +++ b/lib-android-compiler/src/main/java/com/duy/android/compiler/project/AndroidProjectManager.java @@ -222,7 +222,7 @@ private void copyAssets(String assetsPath, File outFile) throws IOException { outFile.getParentFile().mkdirs(); FileOutputStream output = new FileOutputStream(outFile); InputStream input = context.getAssets().open(assetsPath); - org.apache.commons.io.IOUtils.copy(input, output); + IOUtils.copy(input, output); input.close(); output.close(); } @@ -230,7 +230,7 @@ private void copyAssets(String assetsPath, File outFile) throws IOException { private void saveFile(File file, String content) throws IOException { file.getParentFile().mkdirs(); FileOutputStream output = new FileOutputStream(file); - org.apache.commons.io.IOUtils.write(content, output); + IOUtils.write(content, output); output.close(); } diff --git a/lib-android-compiler/src/main/java/com/duy/android/compiler/project/ClassUtil.java b/lib-android-compiler/src/main/java/com/duy/android/compiler/project/ClassUtil.java index e56839bbd..96a9a18e3 100644 --- a/lib-android-compiler/src/main/java/com/duy/android/compiler/project/ClassUtil.java +++ b/lib-android-compiler/src/main/java/com/duy/android/compiler/project/ClassUtil.java @@ -1,5 +1,7 @@ package com.duy.android.compiler.project; +import com.duy.common.io.IOUtils; + import java.io.File; import java.io.FileInputStream; import java.util.regex.Pattern; @@ -24,7 +26,7 @@ public class ClassUtil { */ public static boolean hasMainFunction(File file) { try { - String s = org.apache.commons.io.IOUtils.toString(new FileInputStream(file)); + String s = IOUtils.toString(new FileInputStream(file)); return hasMainFunction(s); } catch (Exception e) { e.printStackTrace(); diff --git a/lib-android-compiler/src/main/java/com/duy/android/compiler/repo/maven/ArtifactDownloader.java b/lib-android-compiler/src/main/java/com/duy/android/compiler/repo/maven/ArtifactDownloader.java deleted file mode 100644 index 093a51803..000000000 --- a/lib-android-compiler/src/main/java/com/duy/android/compiler/repo/maven/ArtifactDownloader.java +++ /dev/null @@ -1,272 +0,0 @@ -/** - * Copyright [2009] Marc-Andre Houle - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * http://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.duy.android.compiler.repo.maven; - -import android.util.Log; - -import com.duy.android.compiler.env.Environment; - -import org.apache.maven.artifact.Artifact; -import org.apache.maven.artifact.factory.ArtifactFactory; -import org.apache.maven.artifact.metadata.ArtifactMetadataSource; -import org.apache.maven.artifact.repository.ArtifactRepository; -import org.apache.maven.artifact.repository.DefaultArtifactRepository; -import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout; -import org.apache.maven.artifact.resolver.ArtifactNotFoundException; -import org.apache.maven.artifact.resolver.ArtifactResolutionException; -import org.apache.maven.artifact.resolver.ArtifactResolutionResult; -import org.apache.maven.artifact.resolver.ArtifactResolver; -import org.apache.maven.project.DefaultMavenProjectBuilder; -import org.apache.maven.project.MavenProject; -import org.apache.maven.project.MavenProjectBuilder; -import org.apache.maven.project.ProjectBuildingException; -import org.apache.maven.project.artifact.InvalidDependencyVersionException; -import org.apache.maven.project.artifact.MavenMetadataSource; -import org.codehaus.plexus.archiver.UnArchiver; -import org.codehaus.plexus.archiver.manager.ArchiverManager; -import org.codehaus.plexus.archiver.manager.DefaultArchiverManager; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -/** - * This mojo is designed to download a maven artifact from the repository and - * download them in the specified path. The maven artifact downloaded can also - * download it's dependency or not, based on a parameter. - * - * @author Marc-Andre Houle - */ -public class ArtifactDownloader { - private static final String TAG = "Artifact"; - private final Set artifactToCopy = new HashSet<>(); - - private final String artifactId; - private final String groupId; - private final String version; - private String type; - private String classifier; - - /** - * Location of the file. - */ - private File outputDirectory; - /** - * Will set the output file name to the specified name. Valid only when the dependency depth - * is set to 0. - */ - private String outputFileName; - /** - * Whether to unpack the artifact - */ - private boolean unpack; - - private List remoteRepositories; - private ArtifactResolver resolver; - private ArtifactMetadataSource metadatSource; - private MavenProjectBuilder mavenProjectBuilder; - private ArchiverManager archiverManager; - private ArtifactRepository localRepository; - private ArtifactFactory artifactFactory; - - public ArtifactDownloader(android.content.Context context, MavenProject mavenProject) { - this.artifactId = mavenProject.getArtifactId(); - System.out.println("mavenProject = " + mavenProject); - this.groupId = mavenProject.getGroupId(); - System.out.println("mavenProject = " + mavenProject); - this.version = mavenProject.getVersion(); - System.out.println("mavenProject = " + mavenProject); - this.type = "pom"; - this.classifier = ""; - - this.resolver = new LocalArtifactResolver(); - this.artifactFactory = new LocalArtifactFactory(); - this.archiverManager = new DefaultArchiverManager(); - this.mavenProjectBuilder = new DefaultMavenProjectBuilder(); - this.metadatSource = new MavenMetadataSource(); - - this.localRepository = new DefaultArtifactRepository( - context.getPackageName(), - Environment.getLocalRepositoryDir(context).getAbsolutePath(), - new DefaultRepositoryLayout()); - - remoteRepositories = new ArrayList<>(); - remoteRepositories.add("https://dl.bintray.com/ppartisan/maven/"); - remoteRepositories.add("https://clojars.org/repo/"); - remoteRepositories.add("https://jitpack.io"); - remoteRepositories.add("https://maven.google.com"); - } - - /** - * Will download the specified artifact in the specified directory. - */ - public void execute() throws MojoExecutionException, MojoFailureException { - Artifact artifact = artifactFactory.createArtifactWithClassifier(groupId, artifactId, version, type, classifier); - downloadAndAddArtifact(artifact); - for (Artifact copy : this.artifactToCopy) { - if (this.unpack) { - this.unpackFileToDirectory(copy); - } else { - this.copyFileToDirectory(copy); - } - } - } - - /** - * Download the artifact when possible and copy it to the target directory - * and will fetch the dependency until the specified depth is reached. - * - * @param artifact The artifact to download and set. - */ - private void downloadAndAddArtifact(Artifact artifact) throws MojoFailureException { - this.downloadArtifact(artifact); - this.artifactToCopy.add(artifact); - - Set dependencies = getTransitiveDependency(artifact); - debug("Nummber dependencies : " + dependencies.size()); - for (Artifact dependency : dependencies) { - downloadAndAddArtifact(dependency); - } - } - - /** - * Will check if the artifact is in the local repository and download it if - * it is not. - * - * @param artifact The artifact to check if it is present in the local directory. - * @throws MojoFailureException If an error happen while resolving the artifact. - */ - private void downloadArtifact(Artifact artifact) throws MojoFailureException { - try { - resolver.resolve(artifact, remoteRepositories, localRepository); - } catch (ArtifactResolutionException e) { - debug("Artifact could not be resolved.", e); - throw new MojoFailureException("Artifact could not be resolved."); - } catch (ArtifactNotFoundException e) { - debug("Artifact could not be found.", e); - throw new MojoFailureException("Artifact could not be found."); - } - } - - /** - * Will copy the specified artifact into the output directory. - * - * @param artifact The artifact already resolved to be copied. - * @throws MojoFailureException If an error hapen while copying the file. - */ - private void copyFileToDirectory(Artifact artifact) throws MojoFailureException { - File toCopy = artifact.getFile(); - if (toCopy != null && toCopy.exists() && toCopy.isFile()) { - try { - info("Copying file " + toCopy.getName() + " to directory " + outputDirectory); - File outputFile = null; - if (this.outputFileName == null) { - outputFile = new File(outputDirectory, toCopy.getName()); - } else { - outputFile = new File(outputDirectory, this.outputFileName); - } - if (outputFile.exists()) { - outputFile.delete(); - } - FileOutputStream output = new FileOutputStream(outputFile); - FileInputStream input = new FileInputStream(toCopy); - org.apache.commons.io.IOUtils.copy(input, output); - output.close(); - input.close(); - } catch (IOException e) { - debug("Error while copying file", e); - throw new MojoFailureException("Error copying the file : " + e.getMessage()); - } - } else { - throw new MojoFailureException("Artifact file not present : " + toCopy); - } - } - - - private void unpackFileToDirectory(Artifact artifact) throws MojoExecutionException { - if (!this.outputDirectory.exists()) { - this.outputDirectory.mkdirs(); - } - File toUnpack = artifact.getFile(); - if (toUnpack != null && toUnpack.exists() && toUnpack.isFile()) { - try { - UnArchiver unarchiver = this.archiverManager.getUnArchiver(toUnpack); - unarchiver.setSourceFile(toUnpack); - unarchiver.setDestDirectory(this.outputDirectory); - unarchiver.extract(); - } catch (Exception ex) { - throw new MojoExecutionException("Issue while unarchiving", ex); - } - } - } - - /** - * Will fetch a list of all the transitive dependencies for an artifact and - * return a set of those artifacts. - * - * @param artifact The artifact for which transitive dependencies need to be - * downloaded. - * @return The set of dependencies that was dependant. - * @throws MojoFailureException If anything goes wrong when getting transitive dependency. - * Note : Suppress warning used for the uncheck cast of artifact - * set. - */ - @SuppressWarnings("unchecked") - private Set getTransitiveDependency(Artifact artifact) throws MojoFailureException { - try { - Artifact pomArtifact = artifactFactory.createArtifact(artifact.getGroupId(), artifact.getArtifactId(), artifact - .getVersion(), artifact.getClassifier(), "pom"); - MavenProject pomProject = mavenProjectBuilder.buildFromRepository(pomArtifact, this.remoteRepositories, this.localRepository); - Set dependents = pomProject.createArtifacts(this.artifactFactory, null, null); - ArtifactResolutionResult result = resolver.resolveTransitively(dependents, pomArtifact, this.localRepository, this.remoteRepositories, - this.metadatSource, null); - if (result != null) { - debug("Found transitive dependency : " + result); - return result.getArtifacts(); - } - } catch (ArtifactResolutionException e) { - debug("Could not resolved the dependency", e); - throw new MojoFailureException("Could not resolved the dependency : " + e.getMessage()); - } catch (ArtifactNotFoundException e) { - debug("Could not find the dependency", e); - throw new MojoFailureException("Could not find the dependency : " + e.getMessage()); - } catch (ProjectBuildingException e) { - debug("Error Creating the pom project for artifact : " + artifact, e); - throw new MojoFailureException("Error getting transitive dependencies : " + e.getMessage()); - } catch (InvalidDependencyVersionException e) { - debug("Error Creating the pom project for artifact : " + artifact, e); - throw new MojoFailureException("Error getting transitive dependencies : " + e.getMessage()); - } - return null; - } - - private void debug(String s, Exception e) { - Log.d(TAG, s, e); - } - - private void debug(String message) { - Log.d(TAG, message); - } - - private void info(String message) { - Log.i(TAG, message); - } -} diff --git a/lib-android-compiler/src/main/java/com/duy/android/compiler/repo/maven/LocalArtifactFactory.java b/lib-android-compiler/src/main/java/com/duy/android/compiler/repo/maven/LocalArtifactFactory.java deleted file mode 100644 index b70734fb6..000000000 --- a/lib-android-compiler/src/main/java/com/duy/android/compiler/repo/maven/LocalArtifactFactory.java +++ /dev/null @@ -1,121 +0,0 @@ -package com.duy.android.compiler.repo.maven; - -import org.apache.maven.artifact.Artifact; -import org.apache.maven.artifact.DefaultArtifact; -import org.apache.maven.artifact.factory.ArtifactFactory; -import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager; -import org.apache.maven.artifact.versioning.VersionRange; - -public class LocalArtifactFactory implements ArtifactFactory { - // TODO: remove, it doesn't know the ones from the plugins - private ArtifactHandlerManager artifactHandlerManager; - - public LocalArtifactFactory() { - - } - - public Artifact createArtifact(String groupId, String artifactId, String version, String scope, String type) { - return createArtifact(groupId, artifactId, version, scope, type, null, null); - } - - public Artifact createArtifactWithClassifier(String groupId, String artifactId, String version, String type, - String classifier) { - return createArtifact(groupId, artifactId, version, null, type, classifier, null); - } - - public Artifact createDependencyArtifact(String groupId, String artifactId, VersionRange versionRange, String type, - String classifier, String scope) { - return createArtifact(groupId, artifactId, versionRange, type, classifier, scope, null); - } - - public Artifact createDependencyArtifact(String groupId, String artifactId, VersionRange versionRange, String type, - String classifier, String scope, boolean optional) { - return createArtifact(groupId, artifactId, versionRange, type, classifier, scope, null, optional); - } - - public Artifact createDependencyArtifact(String groupId, String artifactId, VersionRange versionRange, String type, - String classifier, String scope, String inheritedScope) { - return createArtifact(groupId, artifactId, versionRange, type, classifier, scope, inheritedScope); - } - - public Artifact createDependencyArtifact(String groupId, String artifactId, VersionRange versionRange, String type, - String classifier, String scope, String inheritedScope, boolean optional) { - return createArtifact(groupId, artifactId, versionRange, type, classifier, scope, inheritedScope, optional); - } - - public Artifact createBuildArtifact(String groupId, String artifactId, String version, String packaging) { - return createArtifact(groupId, artifactId, version, null, packaging, null, null); - } - - public Artifact createProjectArtifact(String groupId, String artifactId, String version) { - return createProjectArtifact(groupId, artifactId, version, null); - } - - public Artifact createParentArtifact(String groupId, String artifactId, String version) { - return createProjectArtifact(groupId, artifactId, version); - } - - public Artifact createPluginArtifact(String groupId, String artifactId, VersionRange versionRange) { - return createArtifact(groupId, artifactId, versionRange, "maven-plugin", null, Artifact.SCOPE_RUNTIME, null); - } - - public Artifact createProjectArtifact(String groupId, String artifactId, String version, String scope) { - return createArtifact(groupId, artifactId, version, scope, "pom"); - } - - public Artifact createExtensionArtifact(String groupId, String artifactId, VersionRange versionRange) { - return createArtifact(groupId, artifactId, versionRange, "jar", null, Artifact.SCOPE_RUNTIME, null); - } - - private Artifact createArtifact(String groupId, String artifactId, String version, String scope, String type, - String classifier, String inheritedScope) { - VersionRange versionRange = null; - if (version != null) { - versionRange = VersionRange.createFromVersion(version); - } - return createArtifact(groupId, artifactId, versionRange, type, classifier, scope, inheritedScope); - } - - private Artifact createArtifact(String groupId, String artifactId, VersionRange versionRange, String type, - String classifier, String scope, String inheritedScope) { - return createArtifact(groupId, artifactId, versionRange, type, classifier, scope, inheritedScope, false); - } - - private Artifact createArtifact(String groupId, String artifactId, VersionRange versionRange, String type, - String classifier, String scope, String inheritedScope, boolean optional) { - // TODO: can refactor - inherited scope calculation belongs in the collector, use scope handler - - String desiredScope = Artifact.SCOPE_RUNTIME; - if (inheritedScope == null) { - desiredScope = scope; - } else if (Artifact.SCOPE_TEST.equals(scope) || Artifact.SCOPE_PROVIDED.equals(scope)) { - return null; - } else if (Artifact.SCOPE_COMPILE.equals(scope) && Artifact.SCOPE_COMPILE.equals(inheritedScope)) { - // added to retain compile scope. Remove if you want compile inherited as runtime - desiredScope = Artifact.SCOPE_COMPILE; - } - - if (Artifact.SCOPE_TEST.equals(inheritedScope)) { - desiredScope = Artifact.SCOPE_TEST; - } - - if (Artifact.SCOPE_PROVIDED.equals(inheritedScope)) { - desiredScope = Artifact.SCOPE_PROVIDED; - } - - if (Artifact.SCOPE_SYSTEM.equals(scope)) { - // system scopes come through unchanged... - desiredScope = Artifact.SCOPE_SYSTEM; - } - -// ArtifactHandler handler = artifactHandlerManager.getArtifactHandler(type); - - return new DefaultArtifact(groupId, artifactId, versionRange, desiredScope, type, classifier, /*handler*/null, - optional); - } - - protected ArtifactHandlerManager getArtifactHandlerManager() { - return artifactHandlerManager; - } - -} diff --git a/lib-android-compiler/src/main/java/com/duy/android/compiler/repo/maven/LocalArtifactResolver.java b/lib-android-compiler/src/main/java/com/duy/android/compiler/repo/maven/LocalArtifactResolver.java deleted file mode 100644 index 2388a8c03..000000000 --- a/lib-android-compiler/src/main/java/com/duy/android/compiler/repo/maven/LocalArtifactResolver.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.duy.android.compiler.repo.maven; - -import org.apache.maven.artifact.resolver.DefaultArtifactResolver; - -public class LocalArtifactResolver extends DefaultArtifactResolver { -} diff --git a/lib-android-compiler/src/main/java/com/duy/android/compiler/repo/maven/MojoExecutionException.java b/lib-android-compiler/src/main/java/com/duy/android/compiler/repo/maven/MojoExecutionException.java deleted file mode 100644 index 43e94d587..000000000 --- a/lib-android-compiler/src/main/java/com/duy/android/compiler/repo/maven/MojoExecutionException.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.duy.android.compiler.repo.maven; - -public class MojoExecutionException extends Exception { - public MojoExecutionException(String message, Exception ex) { - super(message, ex); - } -} diff --git a/lib-android-compiler/src/main/java/com/duy/android/compiler/repo/maven/MojoFailureException.java b/lib-android-compiler/src/main/java/com/duy/android/compiler/repo/maven/MojoFailureException.java deleted file mode 100644 index aff03c0fc..000000000 --- a/lib-android-compiler/src/main/java/com/duy/android/compiler/repo/maven/MojoFailureException.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.duy.android.compiler.repo.maven; - -public class MojoFailureException extends Exception { - public MojoFailureException(String message) { - super(message); - } -} diff --git a/lib-android-compiler/src/main/java/com/duy/android/compiler/repo/maven/SignatureUtils.java b/lib-android-compiler/src/main/java/com/duy/android/compiler/repo/maven/SignatureUtils.java deleted file mode 100644 index 991f84fbe..000000000 --- a/lib-android-compiler/src/main/java/com/duy/android/compiler/repo/maven/SignatureUtils.java +++ /dev/null @@ -1,66 +0,0 @@ -/** - * Copyright 2012, Red Hat Inc - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * http://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.duy.android.compiler.repo.maven; - -import org.apache.commons.codec.binary.Hex; - -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; - -/** - * @author Mickael Istria (Red Hat Inc) - */ -public class SignatureUtils { - - public static void verifySignature(File file, String expectedDigest, MessageDigest digest) throws Exception { - String actualDigestHex = SignatureUtils.computeSignatureAsString(file, digest); - if (!actualDigestHex.equals(expectedDigest)) { - throw new MojoFailureException("Not same digest as expected: expected <" + expectedDigest + "> was <" + actualDigestHex + ">"); - } - } - - public static String computeSignatureAsString(File file, - MessageDigest digest) throws IOException { - InputStream fis = new FileInputStream(file); - byte[] buffer = new byte[1024]; - int numRead; - do { - numRead = fis.read(buffer); - if (numRead > 0) { - digest.update(buffer, 0, numRead); - } - } while (numRead != -1); - fis.close(); - byte[] actualDigest = digest.digest(); - return new String(Hex.encodeHex(actualDigest)); - } - - public static String getMD5(File file) throws IOException, NoSuchAlgorithmException { - return computeSignatureAsString(file, MessageDigest.getInstance("MD5")); - } - - public static String getSHA1(File file) throws IOException, NoSuchAlgorithmException { - return computeSignatureAsString(file, MessageDigest.getInstance("SHA1")); - } - - public static String getSHA512(File file) throws IOException, NoSuchAlgorithmException { - return computeSignatureAsString(file, MessageDigest.getInstance("SHA-512")); - } -} diff --git a/lib-android-compiler/src/main/java/com/duy/android/compiler/repo/maven/cache/DownloadCache.java b/lib-android-compiler/src/main/java/com/duy/android/compiler/repo/maven/cache/DownloadCache.java deleted file mode 100644 index e53f7f0be..000000000 --- a/lib-android-compiler/src/main/java/com/duy/android/compiler/repo/maven/cache/DownloadCache.java +++ /dev/null @@ -1,121 +0,0 @@ -/** - * Copyright 2012, Red Hat Inc. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * http://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.duy.android.compiler.repo.maven.cache; - - -import com.duy.android.compiler.repo.maven.SignatureUtils; - -import org.apache.commons.codec.digest.DigestUtils; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.net.URI; -import java.security.MessageDigest; - -/** - * A class representing a download cache - * - * @author Mickael Istria (Red Hat Inc) - */ -public class DownloadCache { - - private final File basedir; - private final FileIndex index; - - public DownloadCache(File cacheDirectory) { - DownloadCache.createIfNeeded(cacheDirectory); - this.index = new FileBackedIndex(cacheDirectory); - this.basedir = cacheDirectory; - } - - private static void createIfNeeded(final File basedir) { - if (!basedir.exists()) { - basedir.mkdirs(); - } else if (!basedir.isDirectory()) { - throw new IllegalArgumentException( - String.format( - "Cannot use %s as cache directory: file is already exist", - basedir - ) - ); - } - } - - private String getEntry(URI uri, String md5, String sha1, String sha512) throws Exception { - if (!this.index.contains(uri)) { - return null; - } - final String res = this.index.get(uri); - File resFile = new File(this.basedir, res); - if (!resFile.isFile()) { - return null; - } - if (md5 != null && !md5.equals(SignatureUtils.getMD5(resFile))) { - return null; - } - if (sha1 != null && !sha1.equals(SignatureUtils.getSHA1(resFile))) { - return null; - } - if (sha512 != null && !sha512.equals(SignatureUtils.getSHA512(resFile))) { - return null; - } - return res; - } - - /** - * Get a File in the download cache. If no cache for this URL, or - * if expected signatures don't match cached ones, returns null. - * available in cache, - * - * @param uri URL of the file - * @param md5 MD5 signature to verify file. Can be null => No check - * @param sha1 Sha1 signature to verify file. Can be null => No check - * @return A File when cache is found, null if no available cache - */ - public File getArtifact(URI uri, String md5, String sha1, String sha512) throws Exception { - String res = getEntry(uri, md5, sha1, sha512); - if (res != null) { - return new File(this.basedir, res); - } - return null; - } - - public void install(URI uri, File outputFile, String md5, String sha1, String sha512) throws Exception { - if (md5 == null) { - md5 = SignatureUtils.computeSignatureAsString(outputFile, MessageDigest.getInstance("MD5")); - } - if (sha1 == null) { - sha1 = SignatureUtils.computeSignatureAsString(outputFile, MessageDigest.getInstance("SHA1")); - } - if (sha512 == null) { - sha512 = SignatureUtils.computeSignatureAsString(outputFile, MessageDigest.getInstance("SHA-512")); - } - String entry = getEntry(uri, md5, sha1, sha512); - if (entry != null) { - return; // entry already here - } - String fileName = outputFile.getName() + '_' + DigestUtils.md5Hex(uri.toString()); -// IOUtils.copy(outputFile.toPath(), new File(this.basedir, fileName).toPath(), StandardCopyOption.REPLACE_EXISTING); - FileOutputStream output = new FileOutputStream(new File(this.basedir, fileName)); - FileInputStream input = new FileInputStream(outputFile); - org.apache.commons.io.IOUtils.copy(input, output); - output.close(); - input.close(); - // update index - this.index.put(uri, fileName); - } -} diff --git a/lib-android-compiler/src/main/java/com/duy/android/compiler/repo/maven/cache/FileBackedIndex.java b/lib-android-compiler/src/main/java/com/duy/android/compiler/repo/maven/cache/FileBackedIndex.java deleted file mode 100644 index 007003c80..000000000 --- a/lib-android-compiler/src/main/java/com/duy/android/compiler/repo/maven/cache/FileBackedIndex.java +++ /dev/null @@ -1,128 +0,0 @@ -package com.duy.android.compiler.repo.maven.cache; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutput; -import java.io.ObjectOutputStream; -import java.io.RandomAccessFile; -import java.net.URI; -import java.nio.channels.FileChannel; -import java.nio.channels.FileLock; -import java.util.HashMap; -import java.util.Map; - -/** - * Binary file backed index. - * @author Paul Polishchuk - * @since 1.3.1 - */ -final class FileBackedIndex implements FileIndex { - - private final Map index = new HashMap<>(); - private final File storage; - - /** - * Creates index backed by file "index.ser" in baseDir. - * Throws runtime exceptions if baseDir does not exist, not a directory - * or file can't be created in it. - * @param baseDir directory where the index file should be stored. - */ - FileBackedIndex(final File baseDir) { - if (!baseDir.isDirectory()) { - throw new IllegalArgumentException( - String.format( - "Cannot use %s as cache directory: not exist or not a directory", - baseDir.getAbsolutePath() - ) - ); - } - final File store = new File(baseDir, "index.ser"); - if (store.exists()) { - this.loadFrom(store); - } else { - FileBackedIndex.create(store); - } - this.storage = store; - } - - @Override - public void put(final URI uri, final String path) { - this.index.put(uri, path); - this.save(); - } - - @Override - public boolean contains(final URI uri) { - this.loadFrom(this.storage); - return this.index.containsKey(uri); - } - - @Override - public String get(final URI uri) { - if (this.contains(uri)) { - return this.index.get(uri); - } - throw new IllegalStateException( - "Cache miss. Check for existence with FileIndex#contains before" - ); - } - - /** - * Create storage file. - * @param store File to be created. - */ - private static void create(final File store) { - try { - if (!store.createNewFile()) { - throw new IllegalStateException( - String.format( - "Failed to create index storage file %s", - store.getAbsolutePath() - ) - ); - } - } catch (final IOException ex) { - throw new IllegalStateException(ex); - } - } - - /** - * Loads index from the file storage replacing all in-memory entries. - * @param store file where index is persisted. - */ - @SuppressWarnings("unchecked") - private void loadFrom(final File store) { - if (store.length() != 0L) { - try ( - final RandomAccessFile file = new RandomAccessFile(store, "r"); - final FileChannel channel = file.getChannel(); - final FileLock lock = channel.lock(0L, Long.MAX_VALUE, true); - final ObjectInputStream deserialize = new ObjectInputStream(new FileInputStream(store)) - ) { - this.index.clear(); - this.index.putAll((Map) deserialize.readObject()); - } catch (final IOException | ClassNotFoundException ex) { - throw new IllegalStateException(ex); - } - } - } - - /** - * Saves current im-memory index to file based storage. - */ - private void save() { - try ( - final FileOutputStream file = new FileOutputStream(this.storage); - final ObjectOutput res = new ObjectOutputStream(file); - final FileChannel channel = file.getChannel(); - final FileLock lock = channel.lock() - ) { - res.writeObject(this.index); - } catch (final IOException ex) { - throw new IllegalStateException(ex); - } - } -} diff --git a/lib-android-compiler/src/main/java/com/duy/android/compiler/repo/maven/cache/FileIndex.java b/lib-android-compiler/src/main/java/com/duy/android/compiler/repo/maven/cache/FileIndex.java deleted file mode 100644 index cd838541b..000000000 --- a/lib-android-compiler/src/main/java/com/duy/android/compiler/repo/maven/cache/FileIndex.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.duy.android.compiler.repo.maven.cache; - -import java.net.URI; - -/** - * Convenient map to search for the path where file is locally stored - * by uri of the resource the file was downloaded from. - * Implementations should not read/write file bodies using stored paths. - * - * @author Paul Polishchuk - * @since 1.3.1 - */ -interface FileIndex { - - /** - * Adds given path to the index using uri parameter as a key. - * @param uri index key - * @param path index value - */ - void put(URI uri, String path); - - /** - * Check if a path associated with the uri key in the index. - *

Use this method before actually trying to get value. - * @param uri index key - * @return true if some path associated with given key - */ - boolean contains(URI uri); - - /** - * Gets stored value by the key. - * @param uri index key - * @return path by given uri key; never NULL - * @throws IllegalStateException in case key is not found - */ - String get(URI uri); -}