Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.ow2.sirocco.cloudmanager.core.utils;
  
  import java.util.List;
 import java.util.Map;
 
 import  javax.ejb.EJBContext;
 import  javax.jms.Connection;
 import  javax.jms.ConnectionFactory;
 import  javax.jms.MessageProducer;
 import  javax.jms.ObjectMessage;
 import  javax.jms.Queue;
 import  javax.jms.Session;
 
 
 public class UtilsForManagers {
     private static Logger logger = LoggerFactory.getLogger(UtilsForManagers.class.getName());

    
This generic method fills a bean with a map of attribute names and attribute values

Parameters:
obj The bean to update
updatedAttributes The map owning attribute names and their respective values
Returns:
Throws:
InstantiationException
IllegalAccessException
IllegalArgumentException
IntrospectionException
NoSuchFieldException
InvocationTargetException
 
     public static Object fillObject(final Object objfinal Map<StringObjectupdatedAttributes)
 
         for (Map.Entry<StringObjectattr : updatedAttributes.entrySet()) {
             UtilsForManagers.invokeSetter(objattr.getKey(), attr.getValue());
         }
 
         return obj;
 
     }

    
This generic method calls a bean setter, given a bean and an attribute name.
It highly relies on reflection

Parameters:
targetObj the bean to update
attrName the name of the attribute to be updated
attrValue the value used to update the attribute
Returns:
Throws:
IntrospectionException
NoSuchFieldException
IllegalArgumentException
IllegalAccessException
InvocationTargetException
 
     private static Object invokeSetter(final Object targetObjfinal String attrNamefinal Object attrValue)
         InvocationTargetException {
 
         BeanInfo info = Introspector.getBeanInfo(targetObj.getClass());
         for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
             if (attrName.equals(pd.getName())) {
                 return pd.getWriteMethod().invoke(targetObjattrValue);
             }
         }
         throw new NoSuchFieldException(targetObj.getClass() + " has no field " + attrName);
     }
 
     public static Object getEntityThroughProxy(Object o) {
         if (o instanceof HibernateProxy) {
             HibernateProxy oProxy = (HibernateProxyo;
             o = oProxy.getHibernateLazyInitializer().getImplementation();
         }
         return o;
 
     }

    
Emits a message to set a listener on the connector task tied to the given Job
The main goal is to ensure that the job listener is triggered after commit, or never triggered if the transaction is rollbacked

Parameters:
payload the related Job
ctx
Throws:
Exception
    public static void emitJobListenerMessage(final Serializable payloadfinal EJBContext ctxthrows Exception {
        UtilsForManagers.emitJMSMessage(payloadctx"jms/JobEmission", 0, 0);
    }

    
emits an JMS message to a queue, inside a JTA transaction

Parameters:
payload the message body
ctx to send the message inside the ctx transaction
queueName
Throws:
Exception
    public static void emitJMSMessage(final Serializable payloadfinal EJBContext ignoredfinal String queueName,
        final long delayMillisfinal long deliveriesCounterthrows Exception {
        InitialContext ctx = new InitialContext();
        ConnectionFactory cf = (ConnectionFactory) ctx.lookup("jms/QueueConnectionFactory");
        Queue queue = (Queue) ctx.lookup(queueName);
        Connection conn = cf.createConnection();
        Session sess = conn.createSession(true, Session.AUTO_ACKNOWLEDGE);
        MessageProducer mp = sess.createProducer(queue);
        ObjectMessage msg = sess.createObjectMessage();
        if (delayMillis > 0) {
            msg.setLongProperty("scheduleDate", System.currentTimeMillis() + delayMillis);
        }
        msg.setLongProperty("deliveriesCounter"deliveriesCounter);
        msg.setObject(payload);
        mp.send(msg);
        sess.close();
        conn.close();
    }
    public static <E> E fillResourceAttributes(final E fromfinal List<Stringattributes) {
        E resource = null;
        try {
            resource = (E) from.getClass().newInstance();
        } catch (InstantiationException e) {
            ..error(""e);
        } catch (IllegalAccessException e) {
            ..error(""e);
        }
        for (int i = 0; i < attributes.size(); i++) {
            try {
                PropertyUtils.setSimpleProperty(resourceattributes.get(i),
                    PropertyUtils.getSimpleProperty(fromattributes.get(i)));
            } catch (NoSuchMethodException e) {
                // ignore wrong attribute name
            } catch (IllegalAccessException e) {
                ..info(""e);
            } catch (InvocationTargetException e) {
                ..info(""e);
            }
        }
        return resource;
    }
New to GrepCode? Check out our FAQ X