I want to implement a custom ValueStack in my application by extending the OgnlValueStack class of Struts 2.3.x.
Please let me know how to accomplish this. What classes do I need to extend and implement in my application and how to inject different dependencies using the #Inject annotation?
Update
I have made the changes as suggested earlier. My ValueStackFactory implementation is:
package jp.co.spectrum.insight.core.mvc.factory;
import java.util.Map;
import java.util.Set;
import jp.co.spectrum.insight.core.datamodel.InsightValueStackImpl;
import ognl.MethodAccessor;
import ognl.OgnlRuntime;
import ognl.PropertyAccessor;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.TextProvider;
import com.opensymphony.xwork2.conversion.NullHandler;
import com.opensymphony.xwork2.conversion.impl.XWorkConverter;
import com.opensymphony.xwork2.inject.Container;
import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.ognl.OgnlNullHandlerWrapper;
import com.opensymphony.xwork2.ognl.accessor.CompoundRootAccessor;
import com.opensymphony.xwork2.util.CompoundRoot;
import com.opensymphony.xwork2.util.ValueStack;
import com.opensymphony.xwork2.util.ValueStackFactory;
public class InsightValueStackFactory implements ValueStackFactory {
#Inject
private XWorkConverter xworkConverter;
private CompoundRootAccessor compoundRootAccessor;
#Inject("system")
private TextProvider textProvider;
#Inject
private Container container;
private boolean allowStaticMethodAccess;
/*
private static ValueStackFactory instance;
public static ValueStackFactory getInstance() {
if (instance == null) {
instance = new InsightValueStackFactory();
}
return instance;
}
*/
private static ValueStackFactory factory = new InsightValueStackFactory();
public static void setFactory(ValueStackFactory factoryParam) {
factory = factoryParam;
}
public static ValueStackFactory getFactory() {
return factory;
}
public void setXWorkConverter(XWorkConverter conv) {
this.xworkConverter = conv;
}
public void setTextProvider(TextProvider textProvider) {
this.textProvider = textProvider;
}
#Inject(value="allowStaticMethodAccess", required=true)
public void setAllowStaticMethodAccess(String allowStaticMethodAccess) {
this.allowStaticMethodAccess = "true".equalsIgnoreCase(allowStaticMethodAccess);
}
public ValueStack createValueStack() {
ValueStack stack = new InsightValueStackImpl(xworkConverter, compoundRootAccessor, textProvider, allowStaticMethodAccess);
container.inject(stack);
stack.getContext().put(ActionContext.CONTAINER, container);
return stack;
}
public ValueStack createValueStack(ValueStack stack) {
ValueStack result = new InsightValueStackImpl(stack, xworkConverter, compoundRootAccessor, allowStaticMethodAccess);
container.inject(result);
stack.getContext().put(ActionContext.CONTAINER, container);
return result;
}
public void setContainer(Container container) throws ClassNotFoundException {
Set<String> names = container.getInstanceNames(PropertyAccessor.class);
for (String name : names) {
Class cls = Class.forName(name);
if (cls != null) {
if (Map.class.isAssignableFrom(cls)) {
PropertyAccessor acc = container.getInstance(PropertyAccessor.class, name);
}
OgnlRuntime.setPropertyAccessor(cls, container.getInstance(PropertyAccessor.class, name));
if (compoundRootAccessor == null && CompoundRoot.class.isAssignableFrom(cls)) {
compoundRootAccessor = (CompoundRootAccessor) container.getInstance(PropertyAccessor.class, name);
}
}
}
names = container.getInstanceNames(MethodAccessor.class);
for (String name : names) {
Class cls = Class.forName(name);
if (cls != null) {
OgnlRuntime.setMethodAccessor(cls, container.getInstance(MethodAccessor.class, name));
}
}
names = container.getInstanceNames(NullHandler.class);
for (String name : names) {
Class cls = Class.forName(name);
if (cls != null) {
OgnlRuntime.setNullHandler(cls, new OgnlNullHandlerWrapper(container.getInstance(NullHandler.class, name)));
}
}
if (compoundRootAccessor == null) {
throw new IllegalStateException("Couldn't find the compound root accessor");
}
this.container = container;
}
}
The InsightValueStackImpl class is my customized ValueStack and it extends the OgnlValueStack.
After the changes as suggested earlier, when I start the application, I get the following error:
java.lang.IllegalArgumentException: Wrapped type converter cannot be null
at com.opensymphony.xwork2.ognl.OgnlTypeConverterWrapper.<init>(OgnlTypeConverterWrapper.java:32)
at com.opensymphony.xwork2.ognl.OgnlValueStack.setRoot(OgnlValueStack.java:88)
at com.opensymphony.xwork2.ognl.OgnlValueStack.<init>(OgnlValueStack.java:71)
at jp.co.spectrum.insight.core.datamodel.InsightValueStackImpl.<init>(InsightValueStackImpl.java:86)
at jp.co.spectrum.insight.core.mvc.factory.InsightValueStackFactory.createValueStack(InsightValueStackFactory.java:85)
at jp.co.spectrum.insight.core.mvc.dispatcher.InsightFilterDispatcher.<init>(InsightFilterDispatcher.java:118)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at java.lang.Class.newInstance0(Class.java:355)
at java.lang.Class.newInstance(Class.java:308)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:275)
at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:422)
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:115)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4072)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4726)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
at org.apache.catalina.core.StandardService.start(StandardService.java:525)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
This is because of null XWorkConverter instance.
Please let me know why it is not getting injected.
Thanks in advance
Thanks
I have made the changes as suggested earlier.
My ValueStackFactory implementation is:
package jp.co.spectrum.insight.core.mvc.factory;
import java.util.Map;
import java.util.Set;
import jp.co.spectrum.insight.core.datamodel.InsightValueStackImpl;
import ognl.MethodAccessor;
import ognl.OgnlRuntime;
import ognl.PropertyAccessor;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.TextProvider;
import com.opensymphony.xwork2.conversion.NullHandler;
import com.opensymphony.xwork2.conversion.impl.XWorkConverter;
import com.opensymphony.xwork2.inject.Container;
import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.ognl.OgnlNullHandlerWrapper;
import com.opensymphony.xwork2.ognl.accessor.CompoundRootAccessor;
import com.opensymphony.xwork2.util.CompoundRoot;
import com.opensymphony.xwork2.util.ValueStack;
import com.opensymphony.xwork2.util.ValueStackFactory;
public class InsightValueStackFactory implements ValueStackFactory {
#Inject
private XWorkConverter xworkConverter ;
private CompoundRootAccessor compoundRootAccessor ;
#Inject("system")
private TextProvider textProvider ;
#Inject
private Container container;
private boolean allowStaticMethodAccess;
/*private static ValueStackFactory instance;
public static ValueStackFactory getInstance(){
if(instance==null){
instance = new InsightValueStackFactory();
}
return instance;
}*/
private static ValueStackFactory factory = new InsightValueStackFactory();
public static void setFactory(ValueStackFactory factoryParam) {
factory = factoryParam;
}
public static ValueStackFactory getFactory() {
return factory;
}
public void setXWorkConverter(XWorkConverter conv) {
this.xworkConverter = conv;
}
public void setTextProvider(TextProvider textProvider) {
this.textProvider = textProvider;
}
#Inject(value="allowStaticMethodAccess", required=true)
public void setAllowStaticMethodAccess(String allowStaticMethodAccess) {
this.allowStaticMethodAccess = "true".equalsIgnoreCase(allowStaticMethodAccess);
}
public ValueStack createValueStack() {
ValueStack stack = new InsightValueStackImpl(xworkConverter, compoundRootAccessor, textProvider, allowStaticMethodAccess);
container.inject(stack);
stack.getContext().put(ActionContext.CONTAINER, container);
return stack;
}
public ValueStack createValueStack(ValueStack stack) {
ValueStack result = new InsightValueStackImpl(stack, xworkConverter, compoundRootAccessor, allowStaticMethodAccess);
container.inject(result);
stack.getContext().put(ActionContext.CONTAINER, container);
return result;
}
public void setContainer(Container container) throws ClassNotFoundException {
Set<String> names = container.getInstanceNames(PropertyAccessor.class);
for (String name : names) {
Class cls = Class.forName(name);
if (cls != null) {
if (Map.class.isAssignableFrom(cls)) {
PropertyAccessor acc = container.getInstance(PropertyAccessor.class, name);
}
OgnlRuntime.setPropertyAccessor(cls, container.getInstance(PropertyAccessor.class, name));
if (compoundRootAccessor == null && CompoundRoot.class.isAssignableFrom(cls)) {
compoundRootAccessor = (CompoundRootAccessor) container.getInstance(PropertyAccessor.class, name);
}
}
}
names = container.getInstanceNames(MethodAccessor.class);
for (String name : names) {
Class cls = Class.forName(name);
if (cls != null) {
OgnlRuntime.setMethodAccessor(cls, container.getInstance(MethodAccessor.class, name));
}
}
names = container.getInstanceNames(NullHandler.class);
for (String name : names) {
Class cls = Class.forName(name);
if (cls != null) {
OgnlRuntime.setNullHandler(cls, new OgnlNullHandlerWrapper(container.getInstance(NullHandler.class, name)));
}
}
if (compoundRootAccessor == null) {
throw new IllegalStateException("Couldn't find the compound root accessor");
}
this.container = container;
}
}
The InsightValueStackImpl class is my customized ValueStack and it extends the OgnlValueStack.
After the changes as suggested earlier, when I start the application, I get the following error:
java.lang.IllegalArgumentException: Wrapped type converter cannot be null
at com.opensymphony.xwork2.ognl.OgnlTypeConverterWrapper.(OgnlTypeConverterWrapper.java:32)
at com.opensymphony.xwork2.ognl.OgnlValueStack.setRoot(OgnlValueStack.java:88)
at com.opensymphony.xwork2.ognl.OgnlValueStack.(OgnlValueStack.java:71)
at jp.co.spectrum.insight.core.datamodel.InsightValueStackImpl.(InsightValueStackImpl.java:86)
at jp.co.spectrum.insight.core.mvc.factory.InsightValueStackFactory.createValueStack(InsightValueStackFactory.java:85)
at jp.co.spectrum.insight.core.mvc.dispatcher.InsightFilterDispatcher.(InsightFilterDispatcher.java:118)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at java.lang.Class.newInstance0(Class.java:355)
at java.lang.Class.newInstance(Class.java:308)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:275)
at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:422)
at org.apache.catalina.core.ApplicationFilterConfig.(ApplicationFilterConfig.java:115)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4072)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4726)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
at org.apache.catalina.core.StandardService.start(StandardService.java:525)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
This is because of null XWorkConverter instance.
Please let me know why it is not getting injected.
Thanks in advance
You need to implement a ValueStackFactory and register it in your struts.xml, as follows:
<bean type="com.opensymphony.xwork2.util.ValueStackFactory"
name="yourOgnlValueStackFactory"
class="com.example.YourOgnlValueStackFactory" />
Then, set your implementation as the factory to use, using:
<constant name="struts.valueStackFactory" value="yourOgnlValueStackFactory"/>
Update
I'm not sure if you are able to mix field and method injection like you are doing. Try moving the #Inject annotations back to the setter methods and see if that resolves the issue.
Related
I am trying to parse PDF files in Mapreduce using custom input format class as below:
import java.io.IOException;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.JobContext;
import org.apache.hadoop.mapreduce.RecordReader;
import org.apache.hadoop.mapreduce.TaskAttemptContext;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
public class PdfFileInputFormat extends FileInputFormat<LongWritable, Text> {
#Override
public RecordReader<LongWritable, Text> createRecordReader(InputSplit split,
TaskAttemptContext context) throws IOException, InterruptedException {
System.out.println("Entered PdfFileInputFormat class");
return new PdfRecordReader();
}
#Override
protected boolean isSplitable(JobContext context, Path file) {
return false;
}
}
and my pdf reader class is:
package com.pdf.prac;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.RecordReader;
import org.apache.hadoop.mapreduce.TaskAttemptContext;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfReaderContentParser;
import com.itextpdf.text.pdf.parser.SimpleTextExtractionStrategy;
import com.itextpdf.text.pdf.parser.TextExtractionStrategy;
import com.itextpdf.text.pdf.parser.RenderListener;
public class PdfRecordReader extends RecordReader<LongWritable, Text> {
private int flag = 0;
private LongWritable key = null;
private Text value = null;
private PdfReader reader;
private PdfReaderContentParser parser;
private TextExtractionStrategy strategy;
private FSDataInputStream fileIn;
private List<String> records = new ArrayList<String>();
public void initialize(InputSplit genericSplit, TaskAttemptContext context)
throws IOException {
System.out.println("Executing initialize........");
FileSplit split = (FileSplit) genericSplit;
Configuration conf = context.getConfiguration();
final Path file = split.getPath();
FileSystem fs = file.getFileSystem(conf);
this.fileIn = fs.open(split.getPath());
this.reader = new PdfReader(fileIn);
this.parser = new PdfReaderContentParser(reader);
readRecords();
}
public synchronized boolean nextKeyValue() throws IOException {
System.out.println("Executing nextKey........Total Records : " + records.size() + "; Flag : " + (flag++));
int index = 0;
if (key == null) {
key = new LongWritable(index);
} else {
index = (int) key.get();
key.set(++index);
}
if (value == null) {
value = new Text(records.get(index));
} else {
value.set(records.get(index));
}
if (flag == records.size()) {
return false;
} else {
return true;
}
}
#Override
public LongWritable getCurrentKey() {
return key;
}
#Override
public Text getCurrentValue() {
return value;
}
/**
* Get the progress within the split
*/
public float getProgress() {
return 0;
}
public synchronized void close() throws IOException {
if (fileIn != null) {
fileIn.close();
}
}
private void readRecords() throws IOException {
if (reader != null) {
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
strategy = parser.processContent(i, new SimpleTextExtractionStrategy());
if (strategy != null) {
/* String[] content = (strategy.getResultantText()).split("\n");
for (String str : content) {
records.add(str);
}*/
StringTokenizer tokens = new StringTokenizer(strategy.getResultantText(), "\n");
while (tokens.hasMoreTokens()) {
records.add(tokens.nextToken());
}
}
}
reader.close();
}
return;
}
}
But this is giving a class not found exception on com.itextpdf.text.pdf.parser.RenderListener at the runtime on hadoop environment. Appreciate any help on this. All the jar files are correctly added on build path and there are no multiple jars as well.
Where can I find the source code for ActivityTestRule?
You can find it at
https://android.googlesource.com/platform/frameworks/testing/+/android-support-test/rules/src/main/java/android/support/test/rule/ActivityTestRule.java
You have to use branch android-support-test instead of master on platform/frameworks/testing project in AOSP.
You can add ActivityTestRule in code, import library and CTRL+Left Click to decompile.
If you can't import this class, you need to add it to dependencies in your build.gradle
androidTestCompile 'com.android.support.test:rules:0.3'
EDIT:
My result of decompiling:
package android.support.test.rule;
import android.app.Activity;
import android.app.Instrumentation;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.test.InstrumentationRegistry;
import android.support.test.annotation.Beta;
import android.support.test.internal.util.Checks;
import android.support.test.rule.UiThreadTestRule;
import android.util.Log;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
#Beta
public class ActivityTestRule<T extends Activity> extends UiThreadTestRule {
private static final String TAG = "ActivityInstrumentationRule";
private final Class<T> mActivityClass;
private Instrumentation mInstrumentation;
private boolean mInitialTouchMode;
private boolean mLaunchActivity;
private T mActivity;
public ActivityTestRule(Class<T> activityClass) {
this(activityClass, false);
}
public ActivityTestRule(Class<T> activityClass, boolean initialTouchMode) {
this(activityClass, initialTouchMode, true);
}
public ActivityTestRule(Class<T> activityClass, boolean initialTouchMode, boolean launchActivity) {
this.mInitialTouchMode = false;
this.mLaunchActivity = false;
this.mActivityClass = activityClass;
this.mInitialTouchMode = initialTouchMode;
this.mLaunchActivity = launchActivity;
this.mInstrumentation = InstrumentationRegistry.getInstrumentation();
}
protected Intent getActivityIntent() {
return new Intent("android.intent.action.MAIN");
}
protected void beforeActivityLaunched() {
}
protected void afterActivityLaunched() {
}
protected void afterActivityFinished() {
}
public T getActivity() {
if(this.mActivity == null) {
Log.w("ActivityInstrumentationRule", "Activity wasn\'t created yet");
}
return this.mActivity;
}
public Statement apply(Statement base, Description description) {
return new ActivityTestRule.ActivityStatement(super.apply(base, description));
}
public T launchActivity(#Nullable Intent startIntent) {
this.mInstrumentation.setInTouchMode(this.mInitialTouchMode);
String targetPackage = this.mInstrumentation.getTargetContext().getPackageName();
if(null == startIntent) {
startIntent = this.getActivityIntent();
if(null == startIntent) {
Log.w("ActivityInstrumentationRule", "getActivityIntent() returned null using default: Intent(Intent.ACTION_MAIN)");
startIntent = new Intent("android.intent.action.MAIN");
}
}
startIntent.setClassName(targetPackage, this.mActivityClass.getName());
startIntent.addFlags(268435456);
Log.d("ActivityInstrumentationRule", String.format("Launching activity %s", new Object[]{this.mActivityClass.getName()}));
this.beforeActivityLaunched();
this.mActivity = (Activity)this.mActivityClass.cast(this.mInstrumentation.startActivitySync(startIntent));
this.mInstrumentation.waitForIdleSync();
this.afterActivityLaunched();
return this.mActivity;
}
void setInstrumentation(Instrumentation instrumentation) {
this.mInstrumentation = (Instrumentation)Checks.checkNotNull(instrumentation, "instrumentation cannot be null!");
}
void finishActivity() {
if(this.mActivity != null) {
this.mActivity.finish();
this.mActivity = null;
}
}
private class ActivityStatement extends Statement {
private final Statement mBase;
public ActivityStatement(Statement base) {
this.mBase = base;
}
public void evaluate() throws Throwable {
try {
if(ActivityTestRule.this.mLaunchActivity) {
ActivityTestRule.this.mActivity = ActivityTestRule.this.launchActivity(ActivityTestRule.this.getActivityIntent());
}
this.mBase.evaluate();
} finally {
ActivityTestRule.this.finishActivity();
ActivityTestRule.this.afterActivityFinished();
}
}
}
}
I'm using SpringData and Neo4j via REST and I am seeing an exception when I add #Indexed(unique=true). This does not throw an exception in embedded, and it does not throw an exception in REST if I remove the unique=true. It Happens even it the database is empty (no nodes and no indexes).
Any ideas?
package com.mycompany;
import org.springframework.data.neo4j.annotation.GraphId;
import org.springframework.data.neo4j.annotation.Indexed;
import org.springframework.data.neo4j.annotation.NodeEntity;
#NodeEntity
public class Person {
#GraphId
private Long id;
#Indexed(unique=true) private String name; // <<<-- does not error if I remove unique-true)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
#Override
public int hashCode() {
int hash = 7;
hash = 23 * hash + (this.name != null ? this.name.hashCode() : 0);
return hash;
}
#Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Person other = (Person) obj;
if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
return false;
}
return true;
}
}
Here's the exception trace:
java.lang.RuntimeException: Error adding element 10 className com.mycompany.Person to index __types__
at org.neo4j.rest.graphdb.ExecutingRestAPI.addToIndex(ExecutingRestAPI.java:397)
at org.neo4j.rest.graphdb.RestAPIFacade.addToIndex(RestAPIFacade.java:166)
at org.neo4j.rest.graphdb.index.RestIndex.add(RestIndex.java:60)
at org.springframework.data.neo4j.support.typerepresentation.AbstractIndexingTypeRepresentationStrategy.add(AbstractIndexingTypeRepresentationStrategy.java:123)
at org.springframework.data.neo4j.support.typerepresentation.AbstractIndexingTypeRepresentationStrategy.addToTypesIndex(AbstractIndexingTypeRepresentationStrategy.java:115)
at org.springframework.data.neo4j.support.typerepresentation.AbstractIndexingTypeRepresentationStrategy.writeTypeTo(AbstractIndexingTypeRepresentationStrategy.java:63)
at org.springframework.data.neo4j.support.mapping.TRSTypeAliasAccessor.writeTypeTo(TRSTypeAliasAccessor.java:46)
at org.springframework.data.neo4j.support.mapping.TRSTypeAliasAccessor.writeTypeTo(TRSTypeAliasAccessor.java:26)
at org.springframework.data.convert.DefaultTypeMapper.writeType(DefaultTypeMapper.java:199)
at org.springframework.data.convert.DefaultTypeMapper.writeType(DefaultTypeMapper.java:186)
at org.springframework.data.neo4j.support.mapping.Neo4jEntityConverterImpl.write(Neo4jEntityConverterImpl.java:165)
at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister$CachedConverter.write(Neo4jEntityPersister.java:179)
at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister.persist(Neo4jEntityPersister.java:243)
at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister.persist(Neo4jEntityPersister.java:231)
at org.springframework.data.neo4j.support.Neo4jTemplate.save(Neo4jTemplate.java:294)
at org.springframework.data.neo4j.support.Neo4jTemplate.save(Neo4jTemplate.java:288)
at com.mycompany.NeoTest.testSomething(NeoTest.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
And just for completeness, here's the test code:
package com.mycompany;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.neo4j.support.Neo4jTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
import com.mycompany.Person;
import static org.junit.Assert.*;
#ContextConfiguration(locations = "classpath:testContext.xml")
#RunWith(SpringJUnit4ClassRunner.class)
public class NeoTest {
#Autowired
Neo4jTemplate template;
#Test
//#Transactional
public void testSomething() {
Person add = new Person();
add.setName("Jorgan Jorgansen");
add = template.save(add);
Person other = template.findOne(add.getId(), Person.class);
assertNotNull("Retrieved entity is non-null", other);
assertEquals("Retrieved entity equals saved entity", add, other);
}
}
Seems a known bug, see this link.
Anyway the exception is about the __types__ index that has nothing to do with the index you define in your class, it's used by SDN to retrieve objects by class name.
I'm using trying to use a javafx combobox with a cell factory to render the list, I was using the setText on the override updateItem() of my CellList, but I found out when I change a value on the underlying model, that doesnẗ afects the deployed list on the combo box. So I try to make it binding both properties and It's works but when a try to clear the selection I have a Exception. This is the code:
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.util.Callback;
public class BasicComboBoxSample extends Application {
public static void main(String[] args) { launch(args); }
#Override public void start(Stage stage) {
final Employee john = new Employee("John");
final Employee jill = new Employee("Jill");
final Employee jack = new Employee("Jack");
final ComboBox<Employee> cboEmployees = new ComboBox();
cboEmployees.getItems().addAll(john, jill, jack);
cboEmployees.setValue(jill);
Button b = new Button("ChangeName");
b.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent actionEvent) {
john.setName("Maria");
}
});
Button c = new Button("Clear");
c.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent actionEvent) {
cboEmployees.getSelectionModel().clearSelection();
}
});
Button d = new Button("Select First");
d.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent actionEvent) {
cboEmployees.getSelectionModel().select(john);
}
});
Callback<ListView<Employee>, ListCell<Employee>> cellFactory = new Callback<ListView<Employee>, ListCell<Employee>>() {
#Override
public ListCell<Employee> call(ListView<Employee> listView) {
return new EmployeeListCell(); //To change body of implemented methods use File | Settings | File Templates.
}
};
cboEmployees.setButtonCell(new EmployeeListCell());
cboEmployees.setCellFactory(cellFactory);
final StackPane layout = new StackPane();
VBox v = new VBox();
v.getChildren().add(cboEmployees);
v.getChildren().add(b);
v.getChildren().add(c);
v.getChildren().add(d);
layout.getChildren().add(v);
layout.setStyle("-fx-background-color: cornsilk; -fx-padding: 15;");
stage.setScene(new Scene(layout));
stage.show();
}
class Employee {
public Employee(String name) { this.setName(name); }
private SimpleStringProperty name = new SimpleStringProperty("");
String getName() {
return name.get();
}
SimpleStringProperty nameProperty() {
return name;
}
void setName(String name) {
this.name.set(name);
}
}
public class EmployeeListCell extends ListCell<Employee> {
#Override
protected void updateItem(Employee emp, boolean b) {
super.updateItem(emp, b);
if(emp != null){
textProperty().bind(emp.nameProperty());
}
}
}
}
And the Exception was:
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: A bound value cannot be set.
at javafx.beans.property.StringPropertyBase.set(StringPropertyBase.java:157)
at javafx.beans.property.StringPropertyBase.set(StringPropertyBase.java:67)
at javafx.beans.property.StringProperty.setValue(StringProperty.java:84)
at javafx.scene.control.Labeled.setText(Labeled.java:135)
at com.sun.javafx.scene.control.skin.ComboBoxListViewSkin.updateDisplayText(ComboBoxListViewSkin.java:420)
at com.sun.javafx.scene.control.skin.ComboBoxListViewSkin.updateDisplayNode(ComboBoxListViewSkin.java:399)
at com.sun.javafx.scene.control.skin.ComboBoxListViewSkin.getDisplayNode(ComboBoxListViewSkin.java:229)
at com.sun.javafx.scene.control.skin.ComboBoxBaseSkin.updateDisplayArea(ComboBoxBaseSkin.java:125)
at com.sun.javafx.scene.control.skin.ComboBoxBaseSkin.handleControlPropertyChanged(ComboBoxBaseSkin.java:120)
at com.sun.javafx.scene.control.skin.ComboBoxListViewSkin.handleControlPropertyChanged(ComboBoxListViewSkin.java:198)
at com.sun.javafx.scene.control.skin.SkinBase$3.changed(SkinBase.java:282)
at javafx.beans.value.WeakChangeListener.changed(WeakChangeListener.java:107)
at com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:367)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:100)
at javafx.beans.property.ObjectPropertyBase.fireValueChangedEvent(ObjectPropertyBase.java:123)
at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:130)
at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:163)
at javafx.scene.control.ComboBoxBase.setValue(ComboBoxBase.java:148)
at javafx.scene.control.ComboBox.updateValue(ComboBox.java:416)
at javafx.scene.control.ComboBox.access$300(ComboBox.java:166)
at javafx.scene.control.ComboBox$6.changed(ComboBox.java:401)
at com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:367)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:100)
at javafx.beans.property.ReadOnlyObjectWrapper$ReadOnlyPropertyImpl.fireValueChangedEvent(ReadOnlyObjectWrapper.java:195)
at javafx.beans.property.ReadOnlyObjectWrapper.fireValueChangedEvent(ReadOnlyObjectWrapper.java:161)
at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:130)
at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:163)
at javafx.scene.control.SelectionModel.setSelectedItem(SelectionModel.java:101)
at javafx.scene.control.ComboBox$ComboBoxSelectionModel$1.invalidated(ComboBox.java:448)
at com.sun.javafx.binding.ExpressionHelper$SingleInvalidation.fireValueChangedEvent(ExpressionHelper.java:155)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:100)
at javafx.beans.property.ReadOnlyIntegerWrapper$ReadOnlyPropertyImpl.fireValueChangedEvent(ReadOnlyIntegerWrapper.java:195)
at javafx.beans.property.ReadOnlyIntegerWrapper.fireValueChangedEvent(ReadOnlyIntegerWrapper.java:161)
at javafx.beans.property.IntegerPropertyBase.markInvalid(IntegerPropertyBase.java:130)
at javafx.beans.property.IntegerPropertyBase.set(IntegerPropertyBase.java:163)
at javafx.scene.control.SelectionModel.setSelectedIndex(SelectionModel.java:67)
at javafx.scene.control.SingleSelectionModel.updateSelectedIndex(SingleSelectionModel.java:208)
at javafx.scene.control.SingleSelectionModel.clearSelection(SingleSelectionModel.java:67)
at de.thomasbolz.javafx.BasicComboBoxSample$2.handle(BasicComboBoxSample.java:45)
at de.thomasbolz.javafx.BasicComboBoxSample$2.handle(BasicComboBoxSample.java:42)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:69)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:28)
at javafx.event.Event.fireEvent(Event.java:171)
at javafx.scene.Node.fireEvent(Node.java:6863)
at javafx.scene.control.Button.fire(Button.java:179)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:193)
at com.sun.javafx.scene.control.skin.SkinBase$4.handle(SkinBase.java:336)
at com.sun.javafx.scene.control.skin.SkinBase$4.handle(SkinBase.java:329)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:64)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:33)
at javafx.event.Event.fireEvent(Event.java:171)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3328)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3168)
at javafx.scene.Scene$MouseHandler.access$1900(Scene.java:3123)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1563)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2265)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:250)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:173)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:292)
at com.sun.glass.ui.View.handleMouseEvent(View.java:528)
at com.sun.glass.ui.View.notifyMouse(View.java:922)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication$3$1.run(GtkApplication.java:82)
at java.lang.Thread.run(Thread.java:722)
You need to unbind the textProperty in case the emp is null
Add the condition in EmployeeListCell class
else {
textProperty().unbind();
}
I am developing a Web App with following :
Glassfish v3.1.2
Eclipse Juno SR2
JPA EclipseLink2.0
JSF 2.0
I have different set of pages for the normal user and for admin users. While trying to setup a page filter during login i am getting this error in my login bean : javax.faces.FacesException: #{loginBean.login}: java.lang.NullPointerException
My whole login code works without this part
if (uGDB.validateGroup(username, adminGroup)) {
return "home.jsf?faces-redirect=true&includeViewParams=true";
}
return "normalHome.jsf?faces-redirect=true&includeViewParams=true"
;
What I am trying to do here is to get the Group Id of the user who is logging in and check if it is admin or not. And accordingly I want to direct the user to the corresponding page. This is because i have different set of pages for admin users and normal users. I don't want to use the Glassfish Realms because the end user doesn't require it.
Can someone please help me identify where I am going wrong in this. (Please excuse me for stupid mistakes I am just starting with such an development). Thanks a lot in advance!
Below is the code for my loginBean
package beans;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import ejb.UserDaoBean;
import ejb.UserGroupDaoBean;
import model.User;
#ManagedBean(name = "loginBean")
#RequestScoped
public class LoginBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
#EJB
private UserDaoBean uDB;
private UserGroupDaoBean uGDB;
private User userId;
private int adminGroup = 1;
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String login() {
FacesContext context = FacesContext.getCurrentInstance();
if (uDB.validateUser(username)) {
userId = uDB.findUser(username);
context.getExternalContext().getSessionMap().put("userId", userId);
if (uGDB.validateGroup(username, adminGroup)) {
return "home.jsf?faces-redirect=true&includeViewParams=true";
}
return "normalHome.jsf?faces-redirect=true&includeViewParams=true";
} else {
FacesMessage message = new FacesMessage();
message.setSeverity(FacesMessage.SEVERITY_ERROR);
message.setSummary("Username doesn't exists! OR User is trying to login from someone else's account");
context.addMessage("", message);
return null;
}
}
public String logout() {
FacesContext.getCurrentInstance().getExternalContext()
.invalidateSession();
return "logout.jsf?faces-redirect=true";
}
}
Here is the complete error stack from Glassfish log
WARNING: #{loginBean.login}: java.lang.NullPointerException
javax.faces.FacesException: #{loginBean.login}: java.lang.NullPointerException
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:722)
Caused by: javax.faces.el.EvaluationException: java.lang.NullPointerException
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
... 31 more
Caused by: java.lang.NullPointerException
at beans.LoginBean.login(LoginBean.java:49)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.sun.el.parser.AstValue.invoke(AstValue.java:254)
at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:302)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
... 32 more
Here is my controller class
package ejb;
import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import javax.persistence.PersistenceContext;
import model.Group;
import model.User;
import model.UserGroup;
#Stateful
#LocalBean
public class UserGroupDaoBean {
#PersistenceContext(unitName = "myPU")
private EntityManager entityManager;
public UserGroupDaoBean() {
}
public UserGroup createNewUserGroup(int groupId, String username) {
UserGroup newUserGrp = new UserGroup();
User myUsr;
myUsr = entityManager.find(User.class, username);
newUserGrp.setUser(myUsr);
Group myGrp;
myGrp = entityManager.find(Group.class, groupId);
newUserGrp.setGroup(myGrp);
saveNewUsrGrp(newUserGrp);
return newUserGrp;
}
private void saveNewUsrGrp(UserGroup usrGrp) {
entityManager.persist(usrGrp);
entityManager.flush();
}
public boolean checkUsertoGroup(String username, int groupId) {
Group chkGrp;
chkGrp = entityManager.find(Group.class, groupId);
User chkUsr;
chkUsr = entityManager.find(User.class, username);
if (chkGrp != null) {
if (chkUsr != null) {
try {
entityManager.createNamedQuery("findGroupsbyUser")
.setParameter("username", chkUsr)
.setParameter("groupId", chkGrp).getSingleResult();
System.out.println("UserGroup already exists");
return false;
} catch (NoResultException e) {
return true;
}
}
System.out.println("User doesn't exist");
return false;
}
System.out.println("Group doesn't exist");
return false;
}
public void deleteUserGroup(UserGroup userGroup) {
userGroup = entityManager.merge(userGroup);
entityManager.remove(userGroup);
}
public UserGroup update(UserGroup myUserGroup) {
return entityManager.merge(myUserGroup);
}
#SuppressWarnings("unchecked")
public List<UserGroup> getAllUserGroups() {
try {
Query query = entityManager.createNamedQuery("findAllUserGroup");
List<UserGroup> result = (List<UserGroup>) query.getResultList();
return result;
} catch (NoResultException e) {
System.out.println("No Result found");
return null;
}
}
public boolean validateGroup(String username, int groupId) {
try {
UserGroup myGroupId = (UserGroup) entityManager
.createNamedQuery("findGroup")
.setParameter("username", username)
.setParameter("groupId", groupId).getSingleResult();
if (myGroupId != null) {
System.out.println("This user is admin!!!");
return true;
}
} catch (NoResultException e) {
return false;
}
System.out.println("This user is not admin");
return false;
}
}
Below is my entity UserGroup
package model;
import java.io.Serializable;
import javax.persistence.*;
/**
* The persistent class for the UserGroup database table.
*
*/
#NamedQueries({
#NamedQuery(name = "findGroupsbyUser", query = "Select ug.group from UserGroup ug where ug.user=:username AND ug.group=:groupId"),
#NamedQuery(name = "findAllUserGroup", query="Select ug from UserGroup ug"),
#NamedQuery(name = "findAdminGroupId", query = "Select ug from UserGroup ug where ug.user=:username AND ug.group=:groupId"),
})
#Entity
#Table(name="usergroup")
public class UserGroup implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name="RowId" )
private int rowId;
//bi-directional many-to-one association to Group
#ManyToOne
#JoinColumn(name="groupId")
private Group group;
//bi-directional many-to-one association to User
#ManyToOne
#JoinColumn(name="username")
private User user;
public UserGroup() {
}
public int getRowId() {
return this.rowId;
}
public void setRowId(int rowId) {
this.rowId = rowId;
}
public Group getGroup() {
return this.group;
}
public void setGroup(Group group) {
this.group = group;
}
public User getUser() {
return this.user;
}
public void setUser(User user) {
this.user = user;
}
}
Read the stack trace: the NPE is thrown at line 49 of LoginBean.java.
With high probability, uGBD is null, because the EJB annotation is missing. You need to use the #EJB annotation in front of each of the EJBs you are injecting:
#EJB
private UserDaoBean uDB;
#EJB
private UserGroupDaoBean uGDB;
...