package com.test;
import java.util.Hashtable;
import java.util.Properties;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class JmsQueueClient
{
public static void main(String ...strings)
{
try
{
Context getMyContext = null;
try
{
Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory" );
env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
env.put(Context.PROVIDER_URL, "jnp://localhost:1199");
env.put(Context.SECURITY_PRINCIPAL, "guest");
env.put(Context.SECURITY_CREDENTIALS, "guest");
getMyContext = new InitialContext(env);
}
catch (NamingException myNamingException)
{
System.out.println("Error establishing Connection" + ": " + myNamingException.toString());
System.exit(1);
}
ConnectionFactory myJMSFactory = null;
Destination myJMSDest = null;
try
{
myJMSFactory = (ConnectionFactory) getMyContext.lookup("ConnectionFactory");
myJMSDest = (Destination) getMyContext.lookup("queue/myAppQueue");
}
catch (Exception e)
{
System.out.println("Lookup failed: " +e);
System.exit(1);
}
Connection myJMSConnection = myJMSFactory.createConnection();
Session myJMSSession = myJMSConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer myJMSProducer = myJMSSession.createProducer(myJMSDest);
TextMessage message = myJMSSession.createTextMessage();
message.setText("Hello there from publisher");
myJMSProducer.send(message);
}
catch (JMSException e)
{
System.out.println(e);
}
}
}
mardi 15 mars 2011
JMS Queue Producer
Sécurisation du Serveur JBoss
- Sécuriser les Consoles JMX
<!-- web.xml -->
<security-constraint>
<web-resource-collection>
<web-resource-name>HtmlAdaptor</web-resource-name>
<description>An example security config that only allows users with the
role JBossAdmin to access the HTML JMX console web application
</description>
<url-pattern>/*</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>JBossAdmin</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>JBoss JMX Console</realm-name>
</login-config>
<security-role>
<role-name>JBossAdmin</role-name>
</security-role>
Attention cependant, à utiliser le même "security-domain" pour les deux consoles, ce qui n'est pas le cas avec les exemples fournis. Il faut veiller aussi à changer les login et password par défaut dans les fichiers properties :
JBOSS_HOME/server/ports-01/conf/props/jbossws-roles.properties
JBOSS_HOME/server/ports-01/conf/props/jbossws-users.properties
<!-- jboss-web.xml -->
<security-domain>java:/jaas/jmx-console</security-domain>
- Sécuriser le JMX Invoker
Les accès via RMI et twiddle passent par le jmx-invoker. Pour le sécuriser, il faut décommenter la partie proposée en fin du fichier jmx-invoker-service.xml :
<!-- deploy/jmx-invoker-service.xml -->
<descriptors>
<interceptors>
<interceptor code="org.jboss.jmx.connector.invoker.AuthenticationInterceptor"
securityDomain="java:/jaas/jmx-console"/>
</interceptors>
</descriptors>
Un fois cette partie activée, les accès RMI nécessitent du authentification via un ClientLoginModule ou via la connexion à JNDI. Les accès par script (twiddle ou shutdown) nécessitent de renseigner les arguments -u et -p.
- Restrictions réseau :
La sécurité des accès distants peut encore être renforcée par un filtrage au niveau système.
Le jmx-invoker utilise un protocole de communication RMI qui s'appuie sur un invoker jrmp, pooled, iiop ou http. En standard, c'est l'invoker jrmp qui est utilisé. Par conséquent, les accès twiddle ou RMI se font via le port 4444. Si on crée un nouvel invoker, sur un autre port, il est alors possible de faire passer uniquement les invocations JMX par ce nouvel invoker. Il reste alors à paramétrer le firewall système du serveur pour qu'il n'accepte que les adresses IP des postes d'administrateur sur ce port.
Pour créer un nouvel invoker, on duplique l'invoker jrmp, du fichier conf/jboss-service.xml, en changeant le port et le nom du MBean ; puis on modifie le ProxyFactory du JmxInvoker.
<!-- deploy/jmx-invoker-service.xml -->
<mbean code="org.jboss.invocation.jrmp.server.JRMPProxyFactory"
name="jboss.jmx:type=adaptor,name=Invoker,protocol=jrmp,service=proxyFactory">
<depends optional-attribute-name="InvokerName">jboss:service=invoker,type=admin</depends>
</mbean>
<!-- RMI/JRMP invoker for Admin -->
<mbean code="org.jboss.invocation.jrmp.server.JRMPInvoker"
name="jboss:service=invoker,type=admin">
<attribute name="RMIObjectPort">4449</attribute>
<attribute name="ServerAddress">${jboss.bind.address}</attribute>
<depends>jboss:service=TransactionManager</depends>
</mbean>
vendredi 11 mars 2011
Comment créer des EJBs WebService
1- Création de l'interface EJB
2- Création de la classe EJB
Fichier aas_handler.xml
<handler-chains xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee">
<handler-chain>
<handler>
<handler-name>AuthenticationHandler</handler-name>
<handler-class>com.test.AASHandler</handler-class>
</handler>
</handler-chain>
</handler-chains>
package org.test
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import javax.xml.soap.SOAPException;
@WebService
@SOAPBinding(style = Style.RPC)
public interface IUserManagerEJB
{
public void createNewUser(User user) throws SOAPSession}
2- Création de la classe EJB
package org.test
import javax.annotation.Resource;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.jws.HandlerChain;
import javax.jws.WebService;
import javax.xml.namespace.QName;
import javax.xml.soap.SOAPException;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.soap.SOAPMessageContext;
@Stateless
@WebService(endpointInterface = "org.test.IUserManagerEJB")
@Remote(IUserManagerEJB.class)
@HandlerChain(file="aas_handler.xml")
public class UserManagerEJB implements IUserManagerEJB
{
@Resource private WebServiceContext ctx; public void createNewUser(User user) throws SOAPSession
{
// Implementatation de la méthode
}
}
Fichier aas_handler.xml
<handler-chains xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee">
<handler-chain>
<handler>
<handler-name>AuthenticationHandler</handler-name>
<handler-class>com.test.AASHandler</handler-class>
</handler>
</handler-chain>
</handler-chains>
package com.test;
public class AASHandler implements SOAPHandler
{
// Complete Implemented méthode here
}
MBean Service Proxy
MBeanServerConnection server = (MBeanServerConnection) ic.lookup ("jmx/invoker/RMIAdaptor");
ObjectName serviceName = new ObjectName("jboss:service=Gsi");
SystemConfigurationServiceMBean myServiceProxy = JMX.newMBeanProxy(server, serviceName, SystemConfigurationServiceMBean.class, true);
Et maintenant vous pouvez utiliser les méthodes de votre MBean (interface).
ObjectName serviceName = new ObjectName("jboss:service=Gsi");
SystemConfigurationServiceMBean myServiceProxy = JMX.newMBeanProxy(server, serviceName, SystemConfigurationServiceMBean.class, true);
Et maintenant vous pouvez utiliser les méthodes de votre MBean (interface).
jeudi 10 mars 2011
Creation des topic et des Queue JMS
Pour la création des Topic et des queue JMS pour le serveur JBOSS, il suffit de créer un fichier {toto-destination-service.xml} dans le répertoire ${JBOSS_HOME}/server/ports-01/deploy/
<?xml version="1.0" encoding="UTF-8"?>
<server>
<!--My queue, named: myAppQueue -->
<mbean code="org.jboss.mq.server.jmx.Queue" name="jboss.mq.destination:service=Queue,name=myAppQueue">
<depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>
</mbean>
<!-- My topic, named: myAppTopic -->
<mbean code="org.jboss.mq.server.jmx.Topic" name="jboss.mq.destination:service=Topic,name=myAppTopic">
<depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>
</mbean>
</server>
mardi 25 mai 2010
Create Hibernate Session Factory
package com.ictelecom.portal.engine.utils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
public class HibernateSessionFactory
{
/** The single instance of hibernate SessionFactory */
private static org.hibernate.SessionFactory sessionFactory;
/**
* disable contructor to guaranty a single instance
*/
private HibernateSessionFactory() {}
public static SessionFactory getInstance()
{
if (sessionFactory == null)
sessionFactory = sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
return sessionFactory;
}
/**
* Opens a session and will not bind it to a session context
* @return the session
*/
public Session openSession()
{
return sessionFactory.openSession();
}
/**
* Returns a session from the session context. If there is no session in the context it opens a session,
* stores it in the context and returns it.
* This factory is intended to be used with a hibernate.cfg.xml
* including the following property
* name="current_session_context_class">thread This would return
* the current open session or if this does not exist, will create a new
* session
*
* @return the session
*/
public Session getCurrentSession()
{
return sessionFactory.getCurrentSession();
}
/**
* closes the session factory
*/
public static void close()
{
if (sessionFactory != null)
sessionFactory.close();
sessionFactory = null;
}
}
vendredi 5 février 2010
BlackBery Developpement Envirronment
Pour le développement des applications pour Blackbery vous pouvez utiliser le BlackBery Developpement Envirronment JDE disponible à l'adresse suivante :
(http://na.blackberry.com/eng/developers/javaappdev/javadevenv.jsp)
Pour la validation et le test de vos applications vous pouvez utiliser le simulateur disponible aussi sur le site de BlackBery.
Voiçi un Exemple Simple :
(http://na.blackberry.com/eng/developers/javaappdev/javadevenv.jsp)
Pour la validation et le test de vos applications vous pouvez utiliser le simulateur disponible aussi sur le site de BlackBery.
Voiçi un Exemple Simple :
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.component.*;
public class HelloWorld extends UiApplication
{
private MainScreen _screen;
private ButtonField _nextScreen;
public static void main(String[] args)
{
HelloWorld instance = new HelloWorld();
instance.enterEventDispatcher();
}
public HelloWorld()
{
_screen = new MainScreen();
_nextScreen = new ButtonField("Go to Next Screen",ButtonField.FIELD_HCENTER | ButtonField.CONSUME_CLICK);
_nextScreen.setChangeListener(new FieldChangeListener()
{
public void fieldChanged(Field field,int context)
{
pushScreen(new NextScreen());
}
});
_screen.setTitle(new LabelField("Hello World Demo",LabelField.USE_ALL_WIDTH));
_screen.add(new RichTextField("Hello to the BlackBerry World!",Field.NON_FOCUSABLE));
_screen.add(_nextScreen);
pushScreen(_screen);
}
}
class NextScreen extends MainScreen
{
public NextScreen()
{
setTitle(new LabelField("Second Screen !",LabelField.USE_ALL_WIDTH));
add(new RichTextField("This is new screen",Field.NON_FOCUSABLE));
ButtonField _btnGoBack = new ButtonField("Go Back",ButtonField.FIELD_HCENTER | ButtonField.CONSUME_CLICK);
_btnGoBack.setChangeListener(new FieldChangeListener()
{
public void fieldChanged(Field field,int context)
{
UiApplication.getUiApplication().popScreen();
}
});
add(_btnGoBack);
}
}
Inscription à :
Articles (Atom)