Can't access properties file - eclipse-plugin

I've made a plugin from external jar. In this jar there is access to properties file:
final Properties properties = new Properties();
final String fileName = "/" + thisClass.getName() + ".properties";
InputStream inputStream;
try
{
inputStream = thisClass.getResourceAsStream(fileName);
properties.load(inputStream);
}
In my RCP-Application the inputStream is null. I've also exported the default package in the plugin. What's wrong.

I've solved my problem. The solution is to make an Eclipse-BuddyPolicy Entry to the Manifest.mf of the plugin with the external jar.
Eclipse-BuddyPolicy: global
This is described in the Eclipse Help: Platform Plugin Developer Guide-->Reference-->Other reference information-->Third party libraries and classloading

Related

View pdf in JPanel

How to view the pdf in JPanel using PDfBox??
I have the Source Code Like below.
try {
PDDocument inputPDF = PDDocument.load(FilePath);
List<PDPage> AllPages = inputPDF.getDocumentCatalog().getAllPages();
inputPDF.close();
PDPage TestPage = (PDPage)AllPages.get(0);
PDFPagePanel pdfPanel = new PDFPagePanel();
pdfPanel.setPage(TestPage);
pnlRiwayatStatus.add(pdfPanel);
}
catch(Exception e){
Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, e);
}
But thus source code NoClassDefFoundError
The missing class is mentioned in a comment:
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
This shows that you don't have the Apache Commons Logging jar in your class path.
According to the PDFBox web site, though, it is a required dependency:
Minimum Requirements
PDFBox has the following basic dependencies:
Java 6
commons-logging
Commons Logging is a generic wrapper around different logging frameworks, so you’ll either need to also use a logging library like log4j or let commons-logging fall back to the standard java.util.logging API included in the Java platform.
You should consider using Apache Maven for automatic dependency resolution.

Velocity Unable To find Resources

This question is already asked but i am trying the same thing that is in accepted answer
protected static final String RESOURCE_LOADER = classpath.resource.loader.class";
static {
System.out.println("Velocity Initialization Started");
velocityEngine = new VelocityEngine();
velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
velocityEngine.setProperty(RESOURCE_LOADER,ClasspathResourceLoader.class.getName());
try {
velocityEngine.init();
} catch (Exception e) {
LOG.error("Failed to load velocity templates e={}", e);
}
}
my velocity file is in
src/main/resources/velocity/templates/command/name.vm
i am getting templates by following command
template = velocityEngine.getTemplate("velocity/templates/command/GenericState.vm");
It works locally but when bundled in jar it does not work , I have examined the jar it consist of velocity folder
i am using velocity to generated java code
I am having maven project setup and maven is creating jar
try this way it should work.
velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, "class,file");
velocityEngine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, "org.apache.velocity.runtime.log.Log4JLogChute");
velocityEngine.setProperty("runtime.log.logsystem.log4j.logger", "VELLOGGER");
velocityEngine.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
velocityEngine.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.NullLogSystem");
velocityEngine.init();

writing files in exported eclipse rcp product

I am a newbie and I am working on eclipse-rcp and trying to build a address book with data saved in xml files.when I am running the project it is able to read and write into the xml file but when I am exporting it into a rcp product it is only reading the file but not able to write.
I tried searching Google but couldn't find the relevant answers so I turned to SO.
Any suggestions??
Edit This is my method where I am trying to read the file and writing it into xml file
public void writedata() {
try {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Bundle bundle = Platform.getBundle(Activator.PLUGIN_ID);
URL fileURL = bundle.getEntry("/xmlfiles/person.xml");
InputStream inputStream=fileURL.openStream();
Document xmlDocument = builder.parse(inputStream);
..................................................
..................................................
..................................................
TransformerFactory transformerFactory = TransformerFactory
.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(xmlDocument);
Bundle bundle1 = Platform.getBundle(Activator.PLUGIN_ID);
URL fileURL1 = bundle1.getEntry("/xmlfiles/person.xml");
StreamResult result = new StreamResult(new File(FileLocator.resolve(fileURL1).getPath()));
transformer.transform(source, result);
When you run your application from Eclipse it uses the expanded project folder for your plugin. Your XML file is writeable in this location.
When you export as an RCP application your plugin gets packaged up as a plugin jar file with the XML file inside it. You won't be able to write to this file.
For the file to be writeable it needs to be outside your plugin project, either in the RCP application workspace or in an external folder.

Adding a nature on a custom project within an Eclipse plugin fails

I'm developping a plugin to implement a custom project kind with custom nature and builder. Within a new wizard, I execute the following code to create the project:
IProject newProject = ResourcesPlugin.getWorkspace().getRoot()
.getProject(projectName);
IProjectDescription desc = newProject.getWorkspace()
.newProjectDescription(newProject.getName());
desc.setLocationURI(projectLocation);
try {
newProject.create(desc, null);
if (!newProject.isOpen()) {
newProject.open(null);
}
} catch (CoreException e) { (...) }
Now the project is created, I try to add the nature with the following code:
if (!project.hasNature(MyNature.NATURE_ID)) {
IProjectDescription description = project.getDescription();
String[] prevNatures = description.getNatureIds();
String[] newNatures = new String[prevNatures.length + 1];
System.arraycopy(prevNatures, 0, newNatures, 0, prevNatures.length);
newNatures[prevNatures.length] = MyNature.NATURE_ID;
description.setNatureIds(newNatures);
IProgressMonitor monitor = new NullProgressMonitor();
project.setDescription(description, monitor);
}
Here is the content of my plugin.xml file:
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Eclipse
Bundle-SymbolicName: com.myapp.eclipse;singleton:=true
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: com.myapp.eclipse.Activator
Bundle-Vendor: MyApp
Require-Bundle: org.eclipse.ui,
org.eclipse.core.runtime,
org.eclipse.core.resources;bundle-version="3.6.1",
org.eclipse.ui.ide;bundle-version="3.6.2"
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Bundle-ActivationPolicy: lazy
Export-Package: com.myapp.eclipse,
com.myapp.eclipse.builder,
com.myapp.eclipse.nature,
com.myapp.eclipse.saving
All these codes partially work since the project is actually created but there is no .project file within it. I can't see any error.
What did I do wrong and how can I debug the problem?
Thanks for your help.
Thierry
I think this may be because you are setting the project location in the project description. Instead of
desc.setLocationURI(projectLocation);
call
desc.setLocation(null);
The location in the description should only be set if it is not the default.

Annotation JCacheResult is not working in Infinispan and Glassfish 3.1.1

I am trying to integrate JCache from Infinispan into my existing EJB project.
I have added Infinispan 5.0.1 CDI and Core packages to Maven pom.
Added Infinispan Interceptors in beans.xml and able to use the CacheResult annotation.
I am deploying the app in Glassfish 3.1.1. I have checked the Weld jar version, which is
Module : org.jboss.weld.osgi-bundle:1.1.1.Final
In the runtime, the CacheResult Method interceptor is not caching the method result and its always called.
My code looks like this,
public void cacheTest() {
Thread.currentThread().setContextClassLoader(
this.getClass().getClassLoader());
EmbeddedCacheManager manager = createCacheConfig();
Set<String> cacheList = manager.getCacheNames(); // new
// DefaultCacheManager().getCacheNames();
for (String cache : cacheList) {
System.out.println("Cache name " + cache);
}
defaultCache = manager.getCache("test-cache");
defaultCache.put("aa", "AA");
String user = "User";
greet(user);
Set<String> keys = defaultCache.keySet();
for (String key : keys) {
System.out.println("Key is -" + key + "Value is -"
+ defaultCache.get(key));
}
}
#CacheResult(cacheName = "test-cache")
public String greet(#CacheKeyParam String user) {
user += "Hello";
return user;
}
public EmbeddedCacheManager createCacheConfig() {
EmbeddedCacheManager manager = new DefaultCacheManager();
Configuration conf = new Configuration();
conf.fluent().eviction().strategy(EvictionStrategy.FIFO).maxEntries(10)
.expiration().maxIdle(1200000L).build();
conf.fluent().clustering().sync();
manager.start();
manager.defineConfiguration("test-cache", conf);
return manager;
}
greet() method gets called but it will never add the method result to the test-cache. I feel am I missing some configuration or...I dont know. Please help me on this.
when I Inject the classes, they wont get constructed and they are null. The code is like this,
#Inject
private static org.infinispan.Cache<String, String> defaultCache;
#Inject
private static EmbeddedCacheManager defaultCacheManager;
These gets executed without any error, but they wont get initialized.
I have no clue...But I am able to inject other EJBs with in this class easily. By the way I am trying to add Jcache functionality in one of EJBs.
I would appreciate your help...
Thanks...
Raj S
Your greet method is in a CDI bean or in an EJB, right?
The cache defined in JCache annotations is looked up in the cache manager provided by Infinispan CDI. This cache manager contains the cache configured with CDI (for more information, see https://docs.jboss.org/author/display/ISPN/CDI+Support). In your example the test-cache configuration will have no effect.
Another thing, if your cacheTest and greet methods are in the same class the greet method cannot be intercepted. If that's not the case maybe you're hitting GLASSFISH-17184.
For the Cache and EmbeddedCacheManager injections the problem is that you're doing a static injection, not supported by CDI. From CDI (JSR-299) specification
An injected field is a non-static, non-final field of a bean class, or of any Java EE component class supporting injection.
If your method result is not cached, I think it's because the CacheResultInterceptor is not called. I've just made the test with the Infinispan CDI quickstart. If the interceptors are in a lib they are not enabled. I think it's a bug in Glassfish.
Btw, you can see an example of code in the Infinispan CDI quickstart here.
Hope this help!