Importing "outside" java class in embedeed OSGi equinox framework - osgi

I have to embed Eclipse/Equinox OSGi Framework to existing application.
I found this tutorial http://njbartlett.name/2011/03/07/embedding-osgi.html and try to use FRAMEWORK_SYSTEMPACKAGES_EXTRA Constans.
So I try export a simple java class into running framework by system bundle, but I receive following error from my activator bundle
Exception in thread "main" org.osgi.framework.BundleException: The activator com.bundleactivator.Activator for bundle com.bundleactivator is invalid
at org.eclipse.osgi.framework.internal.core.AbstractBundle.loadBundleActivator(AbstractBundle.java:156)
at org.eclipse.osgi.framework.internal.core.BundleContextImpl.start(BundleContextImpl.java:751)
at org.eclipse.osgi.framework.internal.core.BundleHost.startWorker(BundleHost.java:370)
at org.eclipse.osgi.framework.internal.core.AbstractBundle.start(AbstractBundle.java:284)
at org.eclipse.osgi.framework.internal.core.AbstractBundle.start(AbstractBundle.java:276)
at RunNewEquinoxOsgi.main(RunNewEquinoxOsgi.java:74)
Caused by: java.lang.Error: Unresolved compilation problem:
The import example cannot be resolved
at com.bundleactivator.Activator.<init>(Activator.java:8)
at java.lang.J9VMInternals.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1505)
at org.eclipse.osgi.framework.internal.core.AbstractBundle.loadBundleActivator(AbstractBundle.java:151)
... 5 more
Exceptions are thrown in the place where I import the example.osgi package from system bundle in the activator
import example.osgi.Example;
I launch equinox by FrameworkFactory in other java class
import java.util.HashMap;
import java.util.Map;
import java.util.ServiceLoader;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.launch.Framework;
import org.osgi.framework.launch.FrameworkFactory;
import example.osgi.*;
FrameworkFactory frameworkFactory = ServiceLoader.load(FrameworkFactory.class).iterator().next();
Map<String, String> config = new HashMap<String, String>();
config.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA,
"example.osgi;version=\"1.0.0\"");
try {
framework.start();
System.out.println("Framework start");
} catch (BundleException e) {
e.printStackTrace();
}
BundleContext ctx = framework.getBundleContext();
Bundle b3 = ctx.installBundle("file:C:\\equinoxtest\\ds.jar");
System.out.println(b3.getBundleId() + " installed");
Bundle b4 = ctx.installBundle("file:C:\\equinoxtest\\services.jar");
System.out.println(b4.getBundleId() + " installed");
Bundle b5 = ctx.installBundle("file:C:\\equinoxtest\\util.jar");
System.out.println(b5.getBundleId() + " installed");
Bundle b6 = ctx.installBundle("file:C:\\equinoxtest\\service.jar");
System.out.println(b6.getBundleId() + " installed");
Bundle b7 = ctx.installBundle("file:C:\\equinoxtest\\rule1.jar");
System.out.println(b7.getBundleId() + " installed");
Bundle b8 = ctx.installBundle("file:C:\\equinoxtest\\rule2.jar");
System.out.println(b8.getBundleId() + " installed");
Bundle b9 = ctx.installBundle("file:C:\\equinoxtest\\rule3.jar");
System.out.println(b9.getBundleId() + " installed");
Bundle b10 = ctx.installBundle("file:C:\\equinoxtest\\activator.jar");
System.out.println(b10.getBundleId() + " installed");
b3.start();
b4.start();
b5.start();
b6.start();
b7.start();
b8.start();
b9.start();
b10.start(); // here i start the activator
This is the manifest of activator class
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: BundleActivator
Bundle-SymbolicName: com.bundleactivator
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: com.bundleactivator.Activator
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Import-Package: com.bundleservice,
example.osgi;version="1.0.0";resolution:=optional,
org.osgi.framework;version="1.3.0",
org.osgi.util.tracker;version="1.4.0"
Bundle-ActivationPolicy: lazy
Export-Package: com.bundleactivator
In the OSGi console i see this package (example.osgi) in system bundle exports, but the importing class(activator) throw Exception. What else should i do?
>osgi packages 0
...
example.osgi; version="1.0.0"<org.eclipse.osgi_3.6.2.R36x_v20110210 [0]>

This error:
java.lang.Error: Unresolved compilation problem
indicates that you are building your code with Eclipse, but there are compilation errors. You need to make your code compile before you run it.
Background info: Eclipse does not use the javac command for compilation, it has a built-in Java compiler known as ECJ. This compiler is designed for incremental use inside the IDE, and it tries its best to keep going even in the presence of errors. So it will still produce a valid .class file, however if you actually try to run something that touches the class(es) with errors in, then you get the above runtime error. In contrast, javac simply stops when it hits a compilation error and will not produce a .class file that you can run.

Related

file.getName() cannot find symbol for method getName() (using Gradle and Javalin)

When i try to run this code:
FileUtils.copyInputStreamToFile(file.getContent(), new File("upload/" + file.getName()));
I get this error:
error: cannot find symbol
symbol: method getName()
location: variable file of type UploadedFile
I have already imported these:
import java.io.*;
import org.apache.commons.io.FileUtils;
I'm working with Gradle and I have already added to the dependencies in build.gradle.kts this one:
implementation(group= "commons-io", name= "commons-io", version= "2.5")
so the build.gradle.kts file builds correctly.
What else should I import? What am I missing?
Javalin's UploadedFile has no property named name. However, it has filename, so this may work (not tested):
FileUtils.copyInputStreamToFile(file.getContent(), new File("upload/" + file.getFilename()));

Groovy #CompileStatic and #TypeChecked order, bug or misunderstanding

I started getting a strange failure when compiling a gradle task class. This is the task I created:
package sample
import groovy.transform.CompileStatic
import groovy.transform.TypeChecked
import org.gradle.api.artifacts.Dependency
import org.gradle.api.provider.Property
import org.gradle.api.tasks.AbstractCopyTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.bundling.Zip
import sample.internal.DataSourceXmlConfig
#TypeChecked
#CompileStatic
class DataSource extends Zip {
#Internal
final Property<File> warFile = project.objects.property(File.class)
DataSource() {
warFile.convention(project.provider {
def files = project.configurations.getByName('warApp').fileCollection { Dependency d ->
d.name == (archiveFileName.getOrElse("") - (~/\.[^.]+$/))
}
files.empty ? null : files.first()
})
}
/**
* This function is used to specify the location of data-sources.xml
* and injects it into the archive
* #param dsConf The configuration object used to specify the location of the
* file as well as any extra variables which should be injected into the file
*/
#Input
void dataSourceXml(#DelegatesTo(DataSourceXmlConfig) Closure dsConf) {
filesToUpdate {
DataSourceXmlConfig ds = new DataSourceXmlConfig()
dsConf.delegate = ds
dsConf.resolveStrategy = Closure.DELEGATE_FIRST
dsConf.call()
exclude('**/WEB-INF/classes/data-sources.xml')
from(ds.source) {
if (ds.expansions) {
expand(ds.expansions)
}
into('WEB-INF/classes/')
rename { 'data-sources.xml' }
}
}
}
private def filesToUpdate(#DelegatesTo(AbstractCopyTask) Closure action) {
action.delegate = this
action.resolveStrategy = Closure.DELEGATE_FIRST
if (warFile.isPresent()) {
from(project.zipTree(warFile)) {
action.call()
}
}
}
}
When groovy compiles this class, I get the following error:
Execution failed for task ':buildSrc:compileGroovy'.
BUG! exception in phase 'class generation' in source unit '/tmp/bus-server/buildSrc/src/main/groovy/sample/DataSource.groovy'
At line 28 column 28 On receiver: archiveFileName.getOrElse() with
message: minus and arguments: .[^.]+$ This method should not have
been called. Please try to create a simple example reproducing this
error and file a bug report at
https://issues.apache.org/jira/browse/GROOVY
Gradle version: 5.6
Groovy version: localGroovy() = 2.5.4
tl;dr, is this a bug or am I missing something about how these annotations work?
The first thing I tried to do was to remove either one of #TypeChecked and #CompileStatic annotations to see if the error goes away.
This actually fixed the problem right away. Compiling the source with either annotations added was successful, but fails when both are present.
I read some questions and answers regarding the use of both annotations, but none of them seemed to suggest that one cannot use both at the same time.
Finally, I tried switching the order of the annotations to see if that helps and to my surprise, it worked! No compilation errors!
This works:
#CompileStatic
#TypeChecked
class DataSource extends Zip { ... }
At this point, I guess my question would be, is this a bug or is there something I am not understanding about the use of both of these annotations? I'm leaning more towards it being a bug just because of the fact that the order made the error message go away.

Maven plugin testing with Groovy: access to generated class?

I'm developping a Maven plugin and some of my tests are done through maven-invoker-plugin based on Groovy scripts (https://maven.apache.org/plugin-developers/plugin-testing.html).
This Maven plugin generates a Java class and I'd like to test this generated class behaviour with Groovy scripts.
I'm facing a problem with these Groovy scripts saying that it could not resolve the class:
Running post-build script: <blah>\verify.groovy
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script1.groovy: 21: unable to resolve class com.mycompany.MyClass
# line 21, column 1.
import com.mycompany.MyClass
Here is the generated Java class from my plugin:
package com.mycompany;
public final class MyClass {
public static String getSomething() {
return "something";
}
}
Here is an extract of my Groovy test verify.groovy:
File generatedJavaFile = new File( basedir, "target/my-plugin/com/mycompany/MyClass.java" );
assert generatedJavaFile.exists()
assert generatedJavaFile.isFile()
File generatedClassFile = new File( basedir, "target/classes/com/mycompany/MyClass.class" );
assert generatedClassFile.exists()
assert generatedClassFile.isFile()
import com.mycompany.MyClass
assert MyClass.getSomething() == "the expected result"
I was wondering if something needs to be specified to the maven-invoker-plugin configuration to include the tested Maven project, or if it's just not possible and I need to find another way...
Thanks,
Benoît.

make tianium module replace R.layout. or R.id in java class

what should I use to replace the R.layout.flash_activity in titanium when building titanium module with ant.
the issue I occured:
error:
jason#jason-Inspiron-3542:/workspace/wechat_login/android$ ant
Buildfile: /workspace/wechat_login/android/build.xml
python.set.exec:
python.check:
[echo] Testing for Python
[exec] Python 2.7.6
init:
process.annotations:
[javac] Compiling 1 source file to /workspace/wechat_login/android/build/classes
[javac] warning: [options] bootstrap class path not set in conjunction with -source 1.6
[javac] warning: The following options were not recognized by any processor: '[kroll.jsonFile, kroll.jsonPackage, kroll.checkTiContext]'
[javac] /workspace/wechat_login/android/src/com/happystock/wxapi/WXEntryActivity.java:18: error: package R does not exist
[javac] setContentView(R.layout.entry);
[javac] ^
[javac] 1 error
[javac] 1 warning
BUILD FAILED
/home/jason/.titanium/mobilesdk/linux/3.5.0.GA/module/android/build.xml:163: Compile failed; see the compiler error output for details.
code:
public class WXEntryActivity extends Activity implements IWXAPIEventHandler{
private IWXAPI api;
private static final String APP_ID = "wx44e8a5248161f***";
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.entry);
api = WXAPIFactory.createWXAPI(this, APP_ID, false);
api.handleIntent(getIntent(), this);
}
Thanks!
Use
int layoutEntry = TiRHelper.getApplicationResource("layout.entry");
setContentView(layoutEntry);
The other thing to make sure is that your resource is actually in platform/android/res/layout/entry.xml or it may not be packaged properly.
I have found a solution on this page that worked for me. You can read more about it there. But if you feel like cutting corners, the code is:
//declaration
View raingBarWrapper;
int resId_raingBarHolder = -1, resId_ratingBar = -1;
//fetching app package name and resources
String packageName = proxy.getActivity().getPackageName();
Resources resources = proxy.getActivity().getResources();
//fetching resource id
resId_raingBarHolder = resources.getIdentifier("raingbar_layout", "layout", packageName);
resId_ratingBar = resources.getIdentifier("ratingbar_default","id", packageName);
LayoutInflater inflater = LayoutInflater.from(getActivity());
//inflating "raingbar_layout" xml file
raingBarWrapper = inflater.inflate(resId_raingBarHolder, null);
//getting reference to RatingBar component in layout
ratingBar = (RatingBar) raingBarWrapper.findViewById(resId_ratingBar);
setNativeView(raingBarWrapper);
//adding properties to RatingBar component
ratingBar.setNumStars(stars);
ratingBar.setStepSize(stepSize);
ratingBar.setRating(rating);

CQ bundle throws exception: only a type can be imported

I have created a sample bundle in CQ5's CRXDE. The process as given on the tutorial was followed to the word.
But when I run the page, it just throws an exception
An error occurred at line: 6 in the generated java file
Only a type can be imported. com.mycompany.test.HelloWorld resolves to a package
The jsp is as below:
<%# page import="com.mycompany.test.HelloWorld"%><%
%><%# include file="/libs/foundation/global.jsp"%><%
%><% HelloWorld hello = new HelloWorld();%><%
%>
<html>
<body>
<b><%= hello.getString() %></b><br>
</body>
</html>
The HelloWorld.java is as follows:
package com.mycompany.test;
public class HelloWorld {
public String getString(){
return "Say Hello to my little friend !!";
}
}
Can't figure out what might be wrong here
Make sure your .bnd file contains the below lines. If not present, you can add them and then build the bundle again.
Export-Package: *
Import-Package: *
Private-Package: *
This error is thrown when your class is not visible in the code.
Make sure
1. Your bundle in the felix (/system/console/bundles) is in active state (shouldn't be in resolved / installed).
2. Your bundle has exported the package com.mycompany.* or *

Resources