[EJB 3.0] - Re: Detach entities - Obtaining a clear pojo
by fatbatman
Thanks, your code works well, I made a few adjustment though to suit my particular needs.
I think it only makes sense to use this function outside the persistance session/transaction. This way you can decide how far you wish to traverse the object graph by populating the data you require within the session, (just as you'd do normally to avoid a LazyInitialisationException). Then when we call the clean function outside the session we traverse until we get a LazyInitializationException, at which point we return null for that field.
I put everything within one class as I don't think we need to force people to extend a base class. I check if an object is an entity by looking for the @Entity annotation. If defining entities in XML this wouldn't work, but I don't :)
.....alternative implementation could get required object to implement some empty interface and check for that, or alternatively I'm sure there is some way of checking using some Hibernate util function, but I don't know what that is.
Its not very well tested but it seems to work for what I need it for at the moment, I'll post bug fixes here as and when they come up.
Let me know your thoughts
James
import java.lang.annotation.Annotation;
| import java.lang.reflect.InvocationTargetException;
| import java.util.ArrayList;
| import java.util.Collection;
| import java.util.HashMap;
| import java.util.HashSet;
| import java.util.List;
| import java.util.Map;
| import java.util.Set;
| import java.util.SortedMap;
| import java.util.SortedSet;
| import java.util.TreeMap;
| import java.util.TreeSet;
|
| import javax.persistence.Entity;
|
| import org.apache.commons.beanutils.PropertyUtils;
| import org.hibernate.LazyInitializationException;
| import org.hibernate.collection.PersistentCollection;
|
| public class HibernateUtils {
|
| public static Object clean(Object obj){
| return removePersistenceContext(obj, new ArrayList<Integer>());
| }
|
| /**
| * Returns a Collection of all objects in the specified persistentCollection
| * without binding to any persistence context or session.
| *
| * @param <T>
| * @param targetCollection
| * @param persistentCollection
| * @return
| */
| public static <T> Collection<T> getCollectionItemsRemovedFromPersistenceContext(
| Collection<T> targetCollection, Collection<T> persistentCollection) throws LazyInitializationException {
| // If runtime type of persistentCollection is not PersistentCollection,
| // take no action
| if (!(persistentCollection instanceof PersistentCollection))
| return persistentCollection;
|
| // Clear existing target
| targetCollection.clear();
|
| // Place all items in persistent collection into target
| for (T item : persistentCollection) {
| targetCollection.add(item);
| }
|
| // Return target
| return targetCollection;
| }
|
| /**
| * Returns a Map of all objects in the specified persistentCollection Map
| * without binding to any persistence context or session.
| *
| * @param <T>
| * @param targetCollection
| * @param persistentCollection
| * @return
| */
| public static <T, U> Map<T, U> getCollectionItemsRemovedFromPersistenceContext(
| Map<T, U> targetMap, Map<T, U> persistentMap) throws LazyInitializationException {
| // If runtime type of persistentCollection is not PersistentCollection,
| // take no action
| if (!(persistentMap instanceof PersistentCollection))
| return persistentMap;
|
| //Clear existing target
| targetMap.clear();
|
| // Place all items in persistent collection into target
| for (T key : persistentMap.keySet()) {
| targetMap.put(key, persistentMap.get(key));
| }
|
| // Return target
| return targetMap;
| }
|
|
|
| /**
| * Checks if the object is an Entity bean by searching for the presence of
| * the @Entity tag in the objects class.
| * NOTE - If you are not using annotations to define your entity objects this function will not work
| * and require an alternative implementation.
| *
| */
| protected static boolean isEntityBean(Object obj){
| for(Annotation a : obj.getClass().getAnnotations()){
| if(a.annotationType()==Entity.class){
| return true;
| }
| }
| return false;
| }
|
|
| /**
| * If the specified object's identity hash code is not in the specified
| * collection of visited hash codes, removes of all data binding the object
| * (and its members) to a specific persistence context, leaving intact only
| * model-centric data
| *
| * @param visitedObjectHashCodes
| * @param obj
| * @author ALR
| */
| protected static Object removePersistenceContext(Object obj, Collection<Integer> visitedObjectHashCodes ){
| if(obj==null){
| return null;
| }
|
| if(visitedObjectHashCodes.contains(System.identityHashCode(obj))){
| return obj;
| }
|
| //Add the object's hash to the Collection of visited hash codes
| visitedObjectHashCodes.add(System.identityHashCode(obj));
|
| try{
| if(obj instanceof Set){
| obj = getCollectionItemsRemovedFromPersistenceContext(new HashSet(), (Set)obj);
| }else if(obj instanceof SortedSet){
| obj = getCollectionItemsRemovedFromPersistenceContext(new TreeSet(), (SortedSet)obj);
| }else if(obj instanceof List){
| obj = getCollectionItemsRemovedFromPersistenceContext(new ArrayList(), (List)obj);
| }else if(obj instanceof Map){
| obj = getCollectionItemsRemovedFromPersistenceContext(new HashMap(), (Map)obj);
| }else if(obj instanceof SortedMap){
| obj = getCollectionItemsRemovedFromPersistenceContext(new TreeMap(), (SortedMap)obj);
| }else if(obj instanceof PersistentCollection){
| obj = getCollectionItemsRemovedFromPersistenceContext(new ArrayList(), (Collection)obj);
| }
| }catch(LazyInitializationException e){
| return null;
| }
|
| if(!isEntityBean(obj)){
| return obj;
| }
|
| Map allMembers = getInternalMembers(obj);
| for(Object member : allMembers.entrySet()){
| Map.Entry m = (Map.Entry)member;
| try {
| try{
| PropertyUtils.setProperty(obj, m.getKey().toString(), removePersistenceContext(m.getValue(), visitedObjectHashCodes));
| }catch (LazyInitializationException e){
| e.printStackTrace();
| PropertyUtils.setProperty(obj, m.getKey().toString(), null);
| }
| } catch (IllegalAccessException e) {
| throw new RuntimeException(e);
| } catch (InvocationTargetException e) {
| throw new RuntimeException(e);
| } catch (NoSuchMethodException e) {
| }
| }
|
| return obj;
| }
|
|
| /**
| * Returns a Map of all internal members of the specified object
| *
| * @param obj
| * The object for which to obtain internal members
| *
| * @author ALR
| * @see http://jakarta.apache.org/commons/beanutils/
| */
| protected static Map getInternalMembers(Object obj){
| try {
| Map map = PropertyUtils.describe(obj);
| return map;
| } catch (IllegalAccessException e) {
| throw new RuntimeException(e);
| } catch (InvocationTargetException e) {
| throw new RuntimeException(e);
| } catch (NoSuchMethodException e) {
| throw new RuntimeException(e);
| }
| }
|
| }
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3969885#3969885
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3969885
19 years, 7 months
[JBossWS] - Re: WS Security with JSR181
by EstrellaRichardson
I still have this problem and I haven't got a clue. If someone needs more info to help me please let me know and I will provide. Just to make things clear about what I need,
1. I need to know why I am getting the warning and how to fix it.
WARN, org.jboss.ws.wsse.WSSecurityHandler, http-0.0.0.0-8080-2] --> Cannot obtain security configuration
2. I need to know how to get JBoss to deploy my service to https and not http.
I am new to implementing webservices so any help would be greatly appreciated.
Thanks again,
Estrella
"EstrellaRichardson" wrote : I am trying to implement WS Security with JSR 181 endpoint. This is my endpoint:
|
| @WebService(name ="InfoService",
| targetNamespace="http://com.tan.app/prov/ws/tan/car",
| serviceName="CarInfo")
| @SOAPBinding(style = SOAPBinding.Style.RPC)
| @Stateless
| @PortComponent(authMethod="BASIC", transportGuarantee="CONFIDENTIAL")
| @SecurityDomain("JBossWS")
| @HandlerChain(file = "resource://config/ServerHandlers.xml", name = "SecureHandlerChain")
| public class CarInfoImpl implements CarInfoService{ code }
|
| Everything deploys and I see the service but when my client (which is unsecure) trys to access it, I get the following message:
| [WARN, org.jboss.ws.wsse.WSSecurityHandler, http-0.0.0.0-8080-2] --> Cannot obtain security configuration
|
|
| I would expect an error message saying that the service couldn't be accessed because it requires WS Security or something of that nature.
|
| Can someone please explain to me what I am doing wrong? I would also expect JBoss to publish the WSDL with a SOAP address location starting with https but mine is http, how do I change this?
|
| Any help would be greatly appreciated as I am facing a really close deadline for this.
|
| Thanks.
|
|
|
|
|
|
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3969884#3969884
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3969884
19 years, 7 months
[JBoss Portal] - Re: I can't see the tabs in my new portal.
by PeterJ
Here is a *-object.xml file that declares a portal with two pages. Each page has a body portlet and the navigation portlet. Place this file in a WEB-INF within a war file and copy to the deploy folder. This is for JBoss Portal 2.4.0.GA.
portalb-object.xml:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
| <deployments>
| <deployment>
| <if-exists>overwrite</if-exists>
| <parent-ref />
| <portal>
| <portal-name>PortalB</portal-name>
| <properties>
| <property>
| <name>portal.defaultObjectName</name>
| <value>Page1</value>
| </property>
| <property>
| <name>layout.id</name>
| <value>generic</value>
| </property>
| <property>
| <name>theme.id</name>
| <value>Nphalanx</value>
| </property>
| <property>
| <name>theme.renderSetId</name>
| <value>divRenderer</value>
| </property>
| <property>
| <name>layout.strategyId</name>
| <value>maximizedRegion</value>
| </property>
| </properties>
| <supported-modes>
| <mode>view</mode>
| <mode>edit</mode>
| <mode>help</mode>
| </supported-modes>
| <supported-window-states>
| <window-state>normal</window-state>
| <window-state>minimized</window-state>
| <window-state>maximized</window-state>
| </supported-window-states>
| <page>
| <page-name>Page1</page-name>
| <window>
| <window-name>Navigation</window-name>
| <instance-ref>NavigationPortletInstance</instance-ref>
| <region>navigation</region>
| <height>0</height>
| <properties>
| <property>
| <name>theme.windowRendererId</name>
| <value>emptyRenderer</value>
| </property>
| <property>
| <name>theme.decorationRendererId</name>
| <value>emptyRenderer</value>
| </property>
| <property>
| <name>theme.portletRendererId</name>
| <value>emptyRenderer</value>
| </property>
| </properties>
| </window>
| <window>
| <window-name>CMS</window-name>
| <instance-ref>CMSPortletInstance</instance-ref>
| <region>center</region>
| <height>0</height>
| </window>
| </page>
| <page>
| <page-name>Page2</page-name>
| <window>
| <window-name>Navigation</window-name>
| <instance-ref>NavigationPortletInstance</instance-ref>
| <region>navigation</region>
| <height>0</height>
| <properties>
| <property>
| <name>theme.windowRendererId</name>
| <value>emptyRenderer</value>
| </property>
| <property>
| <name>theme.decorationRendererId</name>
| <value>emptyRenderer</value>
| </property>
| <property>
| <name>theme.portletRendererId</name>
| <value>emptyRenderer</value>
| </property>
| </properties>
| </window>
| <window>
| <window-name>Image</window-name>
| <instance-ref>WeatherPortletInstance</instance-ref>
| <region>center</region>
| <height>0</height>
| </window>
| </page>
| </portal>
| </deployment>
| </deployments>
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3969883#3969883
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3969883
19 years, 7 months
[JCA/JBoss] - Re: XA Connection error
by smithbstl
I was under the impression that connections and tranasctions were not the same animal. What I am doing is gaining a connection from the datasource looked up from JNDI, issueing an update through that connection, committing the update and closing the connection
public void update(AccountBean acctBean){
| Connection conn = null;
| PreparedStatement ps = null;
| int result = 0;
| String sql = "Update Budget.Account Set Acct_Num = ?, Acct_Name =?" +
| " Where Acct_ID = ?";
| try {
| conn = JDBCUtil.getConnection();
| ps = conn.prepareStatement(sql);
| ps.setString(1,acctBean.getAcctNum());
| ps.setString(2,acctBean.getAcctName());
| ps.setInt(3,acctBean.getAcctId());
| result = ps.executeUpdate();
| conn.commit();
| } catch (SQLException e) {
| System.err.println(e.toString());
| } finally {
| JDBCUtil.close(ps);
| JDBCUtil.close(conn);
| }
| }
JDBCUtil
public static Connection getConnection()
| throws SQLException
| {
| DataSource ds = null;
| Connection connection = null;
| boolean exceptionRaised = false;
| ds = ServiceLocator.getDataSource("java:comp/env/jdbc/XAOracleDS");
| connection = ds.getConnection();
| return connection;
| }
How would this look if I was letting JBoss manage the transaction?
I thought the only difference between TX and XA was the two phase commit?
Thanks for your patience and help.
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3969882#3969882
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3969882
19 years, 7 months
[EJB 3.0] - Re: UndeclaredThrowableException in RC5
by dgreen99
I'm also having a similar problem, however fork="yes" in my Ant script doesn't fix the problem. Here's my stack trace:
java.lang.reflect.UndeclaredThrowableException at $Proxy0.postNotification(Unknown Source) at com.hgea.hch.service.HchClientDelegate.postNotification(HchClientDelegate.java:99) at com.hgea.hch.client.RemoteHchClientTest.testRemotePostNotification(RemoteHchClientTest.java:38)Caused by: java.lang.ClassNotFoundException: [Ljava.lang.StackTraceElement; at java.net.URLClassLoader$1.run(URLClassLoader.java:200) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:188) at java.lang.ClassLoader.loadClass(ClassLoader.java:306) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276) at java.lang.ClassLoader.loadClass(ClassLoader.java:251) at org.jboss.remoting.loading.RemotingClassLoader.loadClass(RemotingClassLoader.java:50) at org.jboss.remoting.loading.ObjectInputStreamWithClassLoader.resolveClass(ObjectInputStreamWithClassLoader.java:139) at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1575) at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1496) at java.io.ObjectInputStream.readArray(ObjectInputStream.java:1624) at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1323) at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1945) at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1869) at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1753) at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329) at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1945) at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1869) at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1753) at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351) at org.jboss.remoting.serialization.impl.java.JavaSerializationManager.receiveObject(JavaSerializationManager.java:128) at org.jboss.remoting.marshal.serializable.SerializableUnMarshaller.read(SerializableUnMarshaller.java:66) at org.jboss.remoting.transport.socket.SocketClientInvoker.transport(SocketClientInvoker.java:279) at org.jboss.remoting.RemoteClientInvoker.invoke(RemoteClientInvoker.java:143) at org.jboss.remoting.Client.invoke(Client.java:525) at org.jboss.remoting.Client.invoke(Client.java:488) at org.jboss.aspects.remoting.InvokeRemoteInterceptor.invoke(InvokeRemoteInterceptor.java:55) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101) at org.jboss.aspects.tx.ClientTxPropagationInterceptor.invoke(ClientTxPropagationInterceptor.java:61) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101) at org.jboss.aspects.security.SecurityClientInterceptor.invoke(SecurityClientInterceptor.java:55) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101) at org.jboss.ejb3.remoting.IsLocalInterceptor.invoke(IsLocalInterceptor.java:65) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101) at org.jboss.ejb3.stateless.StatelessRemoteProxy.invoke(StatelessRemoteProxy.java:102) ... 18 more
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3969874#3969874
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3969874
19 years, 7 months