I created an eclipse plugin that will hook into the save action to create a minified javascript file with the goolge closure compiler. See files below.
That worked until eclipse 3.7.2. Unfortunately now in eclipse 4.2.1 it seems that this creates an endless loop sometimes. The job "compile .min.js" (line 64 in ResourceChangedListener.java) seems the be the cause. It results in the case that the workspaced starts to build over and over. I guess this is because that job creates or changes a file triggering the workspace build again, which again triggers the job which triggers the build and so on.
But I can not figure out how to prevent this.
// Activator.java
package closure_compiler_save;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;
/**
* The activator class controls the plug-in life cycle
*/
public class Activator extends AbstractUIPlugin {
// The plug-in ID
public static final String PLUGIN_ID = "closure-compiler-save"; //$NON-NLS-1$
// The shared instance
private static Activator plugin;
/**
* The constructor
*/
public Activator() {
}
#Override
public void start(BundleContext context) throws Exception {
super.start(context);
Activator.plugin = this;
ResourceChangedListener listener = new ResourceChangedListener();
ResourcesPlugin.getWorkspace().addResourceChangeListener(listener);
}
#Override
public void stop(BundleContext context) throws Exception {
Activator.plugin = null;
super.stop(context);
}
/**
* Returns the shared instance
*
* #return the shared instance
*/
public static Activator getDefault() {
return plugin;
}
}
// ResourceChangedListener.java
package closure_compiler_save;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
public class ResourceChangedListener implements IResourceChangeListener {
public void resourceChanged(IResourceChangeEvent event) {
if (event.getType() != IResourceChangeEvent.POST_CHANGE)
return;
IResourceDelta delta = event.getDelta();
try {
processDelta(delta);
} catch (CoreException e) {
e.printStackTrace();
}
}
// find out which class files were just built
private void processDelta(IResourceDelta delta) throws CoreException {
IResourceDelta[] kids = delta.getAffectedChildren();
for (IResourceDelta delta2 : kids) {
if (delta2.getAffectedChildren().length == 0) {
if (delta.getKind() != IResourceDelta.CHANGED)
return;
IResource res = delta2.getResource();
if (res.getType() == IResource.FILE && "js".equalsIgnoreCase(res.getFileExtension())) {
if (res.getName().contains("min"))
return;
compile(res);
}
}
processDelta(delta2);
}
}
private void compile(final IResource res) throws CoreException {
final IPath fullPath = res.getFullPath();
final IPath fullLocation = res.getLocation();
final String fileName = fullPath.lastSegment().toString();
final String outputFilename = fileName.substring(0, fileName.lastIndexOf(".")).concat(".min.js");
final String outputPath = fullPath.removeFirstSegments(1).removeLastSegments(1).toString();
final IProject project = res.getProject();
final IFile newFile = project.getFile(outputPath.concat("/".concat(outputFilename)));
Job compileJob = new Job("Compile .min.js") {
public IStatus run(IProgressMonitor monitor) {
byte[] bytes = null;
try {
bytes = CallCompiler.compile(fullLocation.toString(), CallCompiler.SIMPLE_OPTIMIZATION).getBytes();
InputStream source = new ByteArrayInputStream(bytes);
if (!newFile.exists()) {
newFile.create(source, IResource.NONE, null);
} else {
newFile.setContents(source, IResource.NONE, null);
}
} catch (IOException e) {
e.printStackTrace();
} catch (CoreException e) {
e.printStackTrace();
}
return Status.OK_STATUS;
}
};
compileJob.setRule(newFile.getProject()); // this will ensure that no two jobs are writing simultaneously on the same file
compileJob.schedule();
}
}
After I setup a blank eclipse classic environment, started a new eclipse plugin project there and recreated all files it works again partly.
In this environment starting a debug session I can save .js files and .min.js files are created automatically.
So far so good!
But when I install the plugin to my real developing eclipse environment automatic saving does not work.
At least one step further!
Step 2:
There were some files not included in the build obviously needed, like manifest. No idea why they were deselected.
Anyway it seems just setting up a blank eclipse 4 classic and going through the eclipse plugin wizard fixed my original problem. I still would love to know what was the actual problem...
Related
I am currently using cucumber and the masterthought plugin to generate html reports on test executions. I want to enable future flexibility to configure the cucumber options when running my tests from a gradle script as well as running the cucumber from java code without the need of using plugins on build script.
I have previously used the surefire plugin to run the cucumber and the masterthought plugin to generate the cucumber report
package com.my.domain;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.junit.runner.RunWith;
import org.junit.runners.model.InitializationError;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import cucumber.runtime.RuntimeOptions;
import cucumber.runtime.RuntimeOptionsFactory;
import net.masterthought.cucumber.Configuration;
import net.masterthought.cucumber.ReportBuilder;
import net.masterthought.cucumber.Reportable;
#RunWith(Cucumber.class)
#CucumberOptions(
plugin = {
"json:build/path/cucumber.json" },
features = {
"src/main/resources/features" },
glue = {
"com.my.domain.stepdefinitions" },
dryRun = false,
tags = {"#tag"}
)
public class CucumberJunitRunner {
// TODO: Make Tags, Features, Glue, plugin runtime, and path configurable
private static final String JSON_PLUGIN_PREFIX = "json:";
private static final String PRECONFIGURED_REPORT_OUTPUT_FOLDER_NAME = "cucumber-html-reports";
private static final Optional<String> REPORT_OUTPUT_FOLDER_LOCATION = Optional.ofNullable(null);
private static final boolean SKIP_REPORT_GENERATION = false;
public static void main(String[] args) throws InitializationError {
RuntimeOptions cucumberRuntimeOptions = null;
Optional<String> jsonPluginOutputLocation = Optional.empty();
if(args.length > 0) {
//TODO: USE ARGUMENTS TO INITIALIZE cucumberRuntimeOptions AND jsonPluginOutputLocation
} else {
RuntimeOptionsFactory cucumberRuntimeOptionsFactory = new RuntimeOptionsFactory(CucumberJunitRunner.class);
cucumberRuntimeOptions = cucumberRuntimeOptionsFactory.create();
jsonPluginOutputLocation = cucumberRuntimeOptions.getPluginFormatterNames().stream()
.filter(s -> s.startsWith(JSON_PLUGIN_PREFIX))
.map(s -> s.substring(JSON_PLUGIN_PREFIX.length())).findFirst();
if( ! jsonPluginOutputLocation.isPresent() ) {
throw new RuntimeException(String.format(
"Could not find jsonPluginOutputLocation in plugins from cucumber options: %s",
cucumberRuntimeOptions.getPluginFormatterNames()));
}
}
deletePreviousData(jsonPluginOutputLocation);
runCucumber(cucumberRuntimeOptions, Thread.currentThread().getContextClassLoader());
if (SKIP_REPORT_GENERATION) {
System.out.println("Report generation skipped. No HTML report was built.");
} else {
if (cucumberRuntimeOptions.isDryRun()) {
System.out.println("Cucumber DryRun executed. No HTML report was built.");
} else {
if (jsonPluginOutputLocation.isPresent()) {
Path jsonPluginOutputPath = Paths.get(jsonPluginOutputLocation.get());
if (Files.exists(jsonPluginOutputPath)) {
generateCucumberReport(
REPORT_OUTPUT_FOLDER_LOCATION.orElse(
FilenameUtils.getFullPathNoEndSeparator(jsonPluginOutputLocation.get())),
jsonPluginOutputLocation.get(), "1", "My Project");
} else {
System.out.println("Cucumber JSON report was missing. No HTML report was built.");
}
} else {
System.out.println("Cucumber JSON plugin was missing. No HTML report was built.");
}
}
}
}
private static void deletePreviousData(Optional<String> jsonPluginOutputLocation) {
Path jsonPluginOutputPath = Paths.get(jsonPluginOutputLocation.get());
if (Files.exists(jsonPluginOutputPath)) {
try {
Files.delete(jsonPluginOutputPath);
System.out.println("Cucumber JSON file was deleted: " +
jsonPluginOutputPath.toAbsolutePath().toString());
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("Cucumber JSON file from previous execution was not detected: "
+ jsonPluginOutputPath.toAbsolutePath().toString());
}
Path cucumberReportsFolder = jsonPluginOutputPath.resolveSibling(PRECONFIGURED_REPORT_OUTPUT_FOLDER_NAME);
if (Files.exists(cucumberReportsFolder)) {
try {
FileUtils.deleteDirectory(cucumberReportsFolder.toFile());
System.out.println("Cucumber JSON report was deleted: " +
cucumberReportsFolder.toAbsolutePath().toString());
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("Cucumber JSON report from previous execution was not detected: "
+ cucumberReportsFolder.toAbsolutePath().toString());
}
}
/**
* Launches the Cucumber-JVM command line.
*
* #param argv runtime options. See details in the
* {#code cucumber.api.cli.Usage.txt} resource.
* #param classLoader classloader used to load the runtime
* #return 0 if execution was successful, 1 if it was not (test failures)
*/
public static byte runCucumber(RuntimeOptions cucumberRuntimeOptions, ClassLoader classLoader) {
final cucumber.runtime.Runtime runtime = cucumber.runtime.Runtime.builder()
.withRuntimeOptions(cucumberRuntimeOptions).withClassLoader(classLoader).build();
runtime.run();
return runtime.exitStatus();
}
private static void generateCucumberReport(String reportOutputDirectoryLocation, String cucumberJsonFile,
String buildNumber, String projectName) {
File reportOutputDirectory = new File(reportOutputDirectoryLocation);
List<String> jsonFiles = new ArrayList<>();
jsonFiles.add(cucumberJsonFile);
// jsonFiles.add("cucumber-report-2.json");
// String buildNumber = "1";
// String projectName = "cucumberProject";
boolean runWithJenkins = false;
Configuration configuration = new Configuration(reportOutputDirectory, projectName);
// optional configuration - check javadoc
configuration.setRunWithJenkins(runWithJenkins);
configuration.setBuildNumber(buildNumber);
// addidtional metadata presented on main page
// configuration.addClassifications("Platform", "Windows");
// configuration.addClassifications("Browser", "Firefox");
// configuration.addClassifications("Branch", "release/1.0");
// optionally add metadata presented on main page via properties file
//List<String> classificationFiles = new ArrayList<>();
//classificationFiles.add("properties-1.properties");
//classificationFiles.add("properties-2.properties");
// configuration.addClassificationFiles(classificationFiles);
ReportBuilder reportBuilder = new ReportBuilder(jsonFiles, configuration);
Reportable result = reportBuilder.generateReports();
// and here validate 'result' to decide what to do if report has failed
if (result == null) {
System.out.println("There was an isssue while building the report");
System.exit(1);
}
System.out.println(result);
}
}
I have gradle script which is creating the version in JIRA using the REST API.
But there is jira-rest-java-client also available. I want to use the java library of jira-rest-java-client and wants to do the same stuff in gradle. Can someone provide an example how could I try this.
How to use the jira-rest-java-client library to make connection with JIRA through example?
In Java I am trying to use this JRCJ Library but getting below error through Intellj
import com.atlassian.jira.rest.client.api.JiraRestClient;
import com.atlassian.jira.rest.client.api.domain.*;
import com.atlassian.jira.rest.client.api.domain.input.ComplexIssueInputFieldValue;
import com.atlassian.jira.rest.client.api.domain.input.FieldInput;
import com.atlassian.jira.rest.client.api.domain.input.TransitionInput;
import com.atlassian.jira.rest.client.internal.ServerVersionConstants;
import com.atlassian.jira.rest.client.internal.async.AsynchronousJiraRestClientFactory;
import com.google.common.collect.Lists;
import org.codehaus.jettison.json.JSONException;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
/**
* A sample code how to use JRJC library
*
* #since v0.1
*/
public class Example1 {
private static URI jiraServerUri = URI.create("http://localhost:2990/jira");
private static boolean quiet = false;
public static void main(String[] args) throws URISyntaxException, JSONException, IOException {
parseArgs(args);
final AsynchronousJiraRestClientFactory factory = new AsynchronousJiraRestClientFactory();
final JiraRestClient restClient = factory.createWithBasicHttpAuthentication(jiraServerUri, "admin", "admin");
try {
final int buildNumber = restClient.getMetadataClient().getServerInfo().claim().getBuildNumber();
// first let's get and print all visible projects (only jira4.3+)
if (buildNumber >= ServerVersionConstants.BN_JIRA_4_3) {
final Iterable<BasicProject> allProjects = restClient.getProjectClient().getAllProjects().claim();
for (BasicProject project : allProjects) {
if (project == TEST){
println(project);}else {
System.out.println("Project" + "Not Found");
}
}
}
// let's now print all issues matching a JQL string (here: all assigned issues)
if (buildNumber >= ServerVersionConstants.BN_JIRA_4_3) {
final SearchResult searchResult = restClient.getSearchClient().searchJql("assignee is not EMPTY").claim();
for (BasicIssue issue : searchResult.getIssues()) {
println(issue.getKey());
}
}
final Issue issue = restClient.getIssueClient().getIssue("TST-7").claim();
println(issue);
// now let's vote for it
restClient.getIssueClient().vote(issue.getVotesUri()).claim();
// now let's watch it
final BasicWatchers watchers = issue.getWatchers();
if (watchers != null) {
restClient.getIssueClient().watch(watchers.getSelf()).claim();
}
// now let's start progress on this issue
final Iterable<Transition> transitions = restClient.getIssueClient().getTransitions(issue.getTransitionsUri()).claim();
final Transition startProgressTransition = getTransitionByName(transitions, "Start Progress");
restClient.getIssueClient().transition(issue.getTransitionsUri(), new TransitionInput(startProgressTransition.getId()))
.claim();
// and now let's resolve it as Incomplete
final Transition resolveIssueTransition = getTransitionByName(transitions, "Resolve Issue");
final Collection<FieldInput> fieldInputs;
// Starting from JIRA 5, fields are handled in different way -
if (buildNumber > ServerVersionConstants.BN_JIRA_5) {
fieldInputs = Arrays.asList(new FieldInput("resolution", ComplexIssueInputFieldValue.with("name", "Incomplete")));
} else {
fieldInputs = Arrays.asList(new FieldInput("resolution", "Incomplete"));
}
final TransitionInput transitionInput = new TransitionInput(resolveIssueTransition.getId(), fieldInputs, Comment
.valueOf("My comment"));
restClient.getIssueClient().transition(issue.getTransitionsUri(), transitionInput).claim();
}
finally {
restClient.close();
}
}
private static void println(Object o) {
if (!quiet) {
System.out.println(o);
}
}
private static void parseArgs(String[] argsArray) throws URISyntaxException {
final List<String> args = Lists.newArrayList(argsArray);
if (args.contains("-q")) {
quiet = true;
args.remove(args.indexOf("-q"));
}
if (!args.isEmpty()) {
jiraServerUri = new URI(args.get(0));
}
}
private static Transition getTransitionByName(Iterable<Transition> transitions, String transitionName) {
for (Transition transition : transitions) {
if (transition.getName().equals(transitionName)) {
return transition;
}
}
return null;
}
}
Error:
xception in thread "main" java.lang.NoClassDefFoundError: com/atlassian/sal/api/executor/ThreadLocalContextManager
at com.atlassian.jira.rest.client.internal.async.AsynchronousJiraRestClientFactory.create(AsynchronousJiraRestClientFactory.java:35)
at com.atlassian.jira.rest.client.internal.async.AsynchronousJiraRestClientFactory.createWithBasicHttpAuthentication(AsynchronousJiraRestClientFactory.java:42)
at Example1.main(Example1.java:34)
Caused by: java.lang.ClassNotFoundException: com.atlassian.sal.api.executor.ThreadLocalContextManager
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 3 more
Moreover I added the JRJC api,core jar in External Libraries but still getting this error?
Could someone tell me what is the issue or where am I missing something?
compile 'com.atlassian.jira:jira-rest-java-client-core:4.0.0'
compile 'com.atlassian.jira:jira-rest-java-client-api:4.0.0'
Simple connection to JIRA:
JiraRestClient restClient = new AsynchronousJiraRestClientFactory().createWithBasicHttpAuthentication(new URI("https://" + jira_domain),
jira_username, jira_password);
I'm trying to run a JavaFx OSGi module, but I keep getting the error:
org.osgi.framework.BundleException: Unable to resolve OSGiDmHelloWorldProvider [7](R 7.0): missing requirement [OSGiDmHelloWorldProvider [7](R 7.0)] osgi.wiring.package; (osgi.wiring.package=javafx.application) Unresolved requirements: [[OSGiDmHelloWorldProvider [7](R 7.0)] osgi.wiring.package; (osgi.wiring.package=javafx.application)]
at org.apache.felix.framework.Felix.resolveBundleRevision(Felix.java:4112)
at org.apache.felix.framework.Felix.startBundle(Felix.java:2118)
at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:998)
at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:984)
This is how I load and start the modules:
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.Constants;
import org.osgi.framework.launch.Framework;
import org.osgi.framework.launch.FrameworkFactory;
import java.io.File;
import java.util.*;
public class Launcher {
private static String[] libs = null;
private BundleContext context;
private Launcher() {
FrameworkFactory frameworkFactory = ServiceLoader.load(FrameworkFactory.class).iterator().next();
Map<String, String> config = new HashMap<String, String>();
config.put("osgi.console", "");
config.put("osgi.clean", "true");
config.put("osgi.noShutdown", "true");
config.put("eclipse.ignoreApp", "true");
config.put("osgi.bundles.defaultStartLevel", "4");
config.put("osgi.configuration.area", "./configuration");
// automated bundles deployment
config.put("felix.fileinstall.dir", "./dropins");
config.put("felix.fileinstall.noInitialDelay", "true");
config.put("felix.fileinstall.start.level", "4");
config.put(Constants.FRAMEWORK_BOOTDELEGATION, "javafx.*,com.sun.javafx.*");
config.put(Constants.FRAMEWORK_BUNDLE_PARENT, Constants.FRAMEWORK_BUNDLE_PARENT_APP);
Framework framework = frameworkFactory.newFramework(config);
try {
framework.init();
framework.start();
} catch (BundleException e) {
e.printStackTrace();
}
context = framework.getBundleContext();
Bundle OSGiDmHelloWorldProvider = install("OSGiDmHelloWorldProvider");
try {
OSGiDmHelloWorldProvider.start();
} catch (BundleException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Launcher();
}
private String[] getLibs() {
if (libs == null) {
List<String> jarsList = new ArrayList<String>();
File pluginsDir = new File("libs");
System.out.println("PATHS : " + pluginsDir.getAbsolutePath());
for (String jar : pluginsDir.list()) {
jarsList.add(jar);
}
libs = jarsList.toArray(new String[jarsList.size()]);
}
return libs;
}
protected Bundle install(String name) {
String found = null;
for (String jar : getLibs()) {
if (jar.startsWith(name)) {
found = String.format("file:libs/%s", jar);
System.out.println(found);
break;
}
}
if (found == null) {
throw new RuntimeException(String.format("JAR for %s not found", name));
}
try {
return context.installBundle(found);
} catch (BundleException e) {
e.printStackTrace();
}
return null;
}
}
The OSGiDmHelloWorldProvider Activator class:
import com.bw.osgi.provider.able.HelloWorldService;
import com.bw.osgi.provider.impl.HelloWorldServiceImpl;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ProviderActivator implements BundleActivator {
Logger logger = LoggerFactory.getLogger(ProviderActivator.class);
Stage stage;
private ServiceRegistration registration;
BundleContext bundleContext;
#Override
public void start(BundleContext bundleContext) throws Exception {
registration = bundleContext.registerService(
HelloWorldService.class.getName(),
new HelloWorldServiceImpl(),
null);
Platform.runLater(new Runnable() {
#Override
public void run() {
stage = new Stage();
BorderPane pane = new BorderPane();
Scene scene = new Scene(pane, 400, 200);
pane.setCenter(new Label("This is a JavaFX Scene in a Stage"));
stage.setScene(scene);
stage.show();
}
});
System.out.println("STAGE CALLED !!!");
}
#Override
public void stop(BundleContext bundleContext) throws Exception {
registration.unregister();
logger.info("Set4Jfx Bundle: stop()");
Platform.runLater(new Runnable() {
#Override
public void run() {
stage.close();
}
});
}
}
How can I load a module that uses JavaFx in a Maven OSGi application?
The error message means that your bundle imports package javafx.application, and no bundle exports that package. Therefore the import cannot be resolved.
I note that in your launcher code you try to set bootdelegation to javafx.*. That might allow the class to be loaded from the boot classpath if your bundle ever got as far as running, however it cannot get that far because of the unresolved import.
If you intend for the JavaFX classes to be loaded from the boot classloader then you should remove the package import from your own bundle. You would also have to arrange for the JavaFX classes to actually be provided by the boot classloader, since AFAIK they are normally only visible from the extension classloader.
However a better solution would be leave the import alone, and arrange for the javafx.application package to be exported from a bundle. That could be done from the system bundle using Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA.
Update
You should probably look at the Drombler FX tool/framework from #Puce, since it sounds like he has already done a lot of these setup tasks. However I felt that his answer didn't directly address your question about why your code was failing.
I've released some first versions of Drombler FX - the modular application framework for JavaFX based on OSGi and Maven (POM first).
It takes care of initializing JavaFX and OSGi.
Maybe you find it useful. The application framework is Open Source and the code is available on GitHub.
There is also a tutorial with a Getting Started trail.
I have some symlinks in my build system that point to jars which I need to build if the jars don't exist. i.e. if the symlinks are dangling. Is there an Ant task or workaround to check that?
As to why I can't include a proper Ant dependency to those jars, the reason is that their build process is lengthy, involving on-the-fly Internet downloads from ftp repositories that take too long and is out of my control.
Ok, so in the end I implemented a custom Ant task (code at the end), that can be used from Ant like this:
<file-pronouncer filePath="path/to/file" retProperty="prop-holding-type-of-that-file"/>
It can then be read with:
<echo message="the file-pronouncer task for file 'path/to/file' returned: ${prop-holding-type-of-that-file}"/>
With the following possible outcomes:
[echo] the file-pronouncer task for file 'a' returned: regular-file
[echo] the file-pronouncer task for file 'b' returned: symlink-ok
[echo] the file-pronouncer task for file 'c' returned: symlink-dangling
[echo] the file-pronouncer task for file 'd' returned: not-exists
code for the FilePronouncer custom Ant task
import java.io.IOException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import java.nio.file.Path;
import java.nio.file.Files;
import java.nio.file.FileSystems;
import java.nio.file.LinkOption;
import java.nio.file.attribute.BasicFileAttributes;
import org.apache.tools.ant.BuildException;
public class FilePronouncer extends Task {
private String filePath = null;
private String retProperty = null;
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public String getRetProperty() {
return retProperty;
}
public void setRetProperty(String property) {
this.retProperty = property;
}
public void execute() {
try {
Path path = FileSystems.getDefault().getPath(filePath);
boolean fileExists = Files.exists(path, LinkOption.NOFOLLOW_LINKS);
Boolean isSymlink = null;
Boolean filePointedToExists = null;
if (fileExists) {
BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
isSymlink = attrs.isSymbolicLink();
if (isSymlink)
filePointedToExists = Files.exists(path);
}
Project project = getProject();
String rv = null;
if (!fileExists)
rv = "not-exists";
else {
if (!isSymlink)
rv = "regular-file";
else {
if (filePointedToExists)
rv = "symlink-ok";
else
rv = "symlink-dangling";
}
}
project.setProperty(retProperty, rv);
} catch (IOException e) {
throw new BuildException(e);
}
}
}
I used a screenshot plugin for ios when I developed an iphone/ipad app. I am now creating an android version of the app and am trying to implement the android version of the plugin.
My java part fo the plugin looks like this:
package org.apache.cordova;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
import org.json.JSONArray;
import android.graphics.Bitmap;
import android.os.Environment;
import android.view.View;
public class Screenshot extends Plugin {
private PluginResult result = null;
#Override
public PluginResult execute(String action, JSONArray args, String callbackId) {
// starting on ICS, some WebView methods
// can only be called on UI threads
super.cordova.getActivity().runOnUiThread(new Runnable() {
public void run() {
View view = webView.getRootView();
view.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
try {
File folder = new File(Environment.getExternalStorageDirectory(), "Pictures");
if (!folder.exists()) {
folder.mkdirs();
}
File f = new File(folder, "screenshot_" + System.currentTimeMillis() + ".png");
FileOutputStream fos = new FileOutputStream(f);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
result = new PluginResult(PluginResult.Status.OK);
} catch (IOException e) {
result = new PluginResult(PluginResult.Status.IO_EXCEPTION, e.getMessage());
}
}
});
// waiting ui thread to finish
while (this.result == null) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// ignoring exception, since we have to wait
// ui thread to finish
}
}
return this.result;
}
}
My Screenshot.js looks like this:
(function() {
/* Get local ref to global PhoneGap/Cordova/cordova object for exec function.
- This increases the compatibility of the plugin. */
var cordovaRef = window.PhoneGap || window.Cordova || window.cordova; // old to new fallbacks
/**
* This class exposes the ability to take a Screenshot to JavaScript
*/
function Screenshot() { }
/**
* Save the screenshot to the user's Photo Library
*/
Screenshot.prototype.saveScreenshot = function() {
cordovaRef.exec(null, null, "Screenshot", "saveScreenshot", []);
};
if (!window.plugins) {
window.plugins = {};
}
if (!window.plugins.screenshot) {
window.plugins.screenshot = new Screenshot();
}
})(); /* End of Temporary Scope. */
Now I try to call my screenshot.js function by using this code:
function takeScreenShot() {
cordovaRef.exec("Screenshot.saveScreenshot");
}
However all I get is JSON errors, I know somewhere im asking to to convert it to JSON from a java string but i just can't figure out how to change it. Ok well I think that is what is wrong...
My errors look like this:
ERROR: org.json.JSONException: Value undefined of type java.lang.String cannot be converted to JSONArray.
Error: Status=8 Message=JSON error
file:///android_asset/www/cordova-2.0.0.js: Line 938 : Error: Status=8 Message=JSON error
Error: Status=8 Message=JSON error at file:///android_asset_/www/cordova-2.0.0.js:938
Can anyone guide me where Im going wrong please?
Question resolved by:
Phonegap Screenshot plugin in Cordova 2.0.0
Answer provided by Simon MacDonald.