vendredi 25 février 2011

J2EE : recupérer des informations sur la mémoire JVM

Il arrive fréquemment d'avoir bsoin d'information sur la consommation mémoire de'une application, pour cela, il existe des moyen simple de récupérer ces infos, en les incluant dans une servlet ou une page jsp.

Voici, ce que fournit la classe Runtime :


long freeMemory = Runtime.getRuntime().freeMemory() ;
long maxMemory = Runtime.getRuntime().maxMemory() ;
long totalMemory = Runtime.getRuntime().totalMemory() ;
long usedMemory = totalMemory - freeMemory ;
double ratio = ( ( (double)usedMemory / (double)maxMemory ) * 100 ) ;


Après il est simple d'utilser CURL ou WGET pour interroger régulièrement ces pages, ou de faire un thread directement dans la pages jsp ou la servlet.

mardi 22 février 2011

J2EE : transformer du XML en une chaine

Tout d'abord, il faut initialiser un transformer et mettre l'option OMIT_XML_DECLARATION pour éviter la déclaration du XML :


tf = TransformerFactory.newInstance();
try {
c = DocumentBuilderFactory.newInstance().newDocumentBuilder();
} catch (ParserConfigurationException e1) {
logger.error("Erreur de configuration du builder XML", e1);
}
try {
transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,
"yes");
} catch (TransformerConfigurationException e) {
logger.error("Erreur d'instantiation du transformer XML", e);
}


Et ensuite la transformation :


String htmlResult;
tf = TransformerFactory.newInstance();
DOMSource domSource = new DOMSource(d);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
transformer.transform(domSource, result);
htmlResult = writer.toString();



Voici l'exemple complet :

package com.generic.description;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.ResourceBundle;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import com.generic.description.mapping.Element;
import com.generic.description.mapping.FieldDes;
import com.generic.front.Init;
import com.generic.link.LinkManager;
import com.generic.project.Project;
import com.generic.project.ProjectManager;
import com.generic.user.User;
import com.generic.util.ReloadablePropertyResourceBundle;

public class PostTraitementHelper {

private static PostTraitementHelper instance = null;

private static final Logger logger = Logger.getLogger(ProjectLoader.class);

private String directory = "";

private HashMap methodCache = new HashMap();

private TransformerFactory tf;

private Transformer transformer;

private DocumentBuilder c;

static {
// initialisation
ResourceBundle rb = ReloadablePropertyResourceBundle
.getResourceBundle(Init.PROP_GENERAL);
String dir = rb.getString(ProjectManager.PROPERTIES_SPECIFIQUES_DIR);
instance = new PostTraitementHelper();
instance.setDirectory(dir);
instance.initActionMap();
}

public static final String NONE = "none";
public static final String POSTTRAITEMENT_PROPERTIES = "posttraitement.properties";

private Map actionMap = new HashMap();
private long lastModification = 0L;

private PostTraitementHelper() {
super();

tf = TransformerFactory.newInstance();
try {
c = DocumentBuilderFactory.newInstance().newDocumentBuilder();
} catch (ParserConfigurationException e1) {
logger.error("Erreur de configuration du builder XML", e1);
}
try {
transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,
"yes");
} catch (TransformerConfigurationException e) {
logger.error("Erreur d'instantiation du transformer XML", e);
}
}

public static PostTraitementHelper getInstance() {
return instance.initActionMap();
}

@SuppressWarnings("unchecked")
public synchronized PostTraitementHelper initActionMap() {
Date debut = new Date();

if (directory == null && "".equals(directory)) {
throw new IllegalArgumentException(String.format(
"l'argument [%s] est invalide", directory));
}

String pathFile = directory + "/"
+ PostTraitementHelper.POSTTRAITEMENT_PROPERTIES;

File f = null;
FileReader r = null;
try {
f = new File(pathFile);

if (f.exists() && f.canRead()) {
if (f.lastModified() > lastModification) {
// le fichier a changé et doit etre relu
lastModification = f.lastModified();

r = new FileReader(f);
Properties properties = new Properties();

if (logger.isDebugEnabled()) {
logger
.debug(String
.format(
"valeur de config du fichier [%s] sous forme [key]=[value]",
POSTTRAITEMENT_PROPERTIES));
}

try {
// lecture des cle
properties.load(r);

// nettoyage
this.actionMap.clear();
this.methodCache.clear();

// ajout du cas par defaut
this.actionMap.put(NONE, NONE);

// iteration et traitement de chaque cle
Enumeration keys = properties.keys();

while (keys.hasMoreElements()) {
String key = (String) keys.nextElement();
String value = properties.getProperty(key, NONE);

if (!this.actionMap.containsKey(key)) {

try {
if (logger.isDebugEnabled()) {
logger.debug(String.format(
"[%s]=[%s] \n", key, value));
}
this.actionMap.put(key, value);
getActionMethod(key);
} catch (SecurityException e) {
logger.error(
"erreur lors de la reflection", e);
this.actionMap.remove(key);
} catch (NoSuchMethodException e) {
logger.error(
"erreur lors de la reflection", e);
this.actionMap.remove(key);
}
} else {
logger
.warn(String
.format(
"the following key [%s] exists in conf file [%s]",
key,
POSTTRAITEMENT_PROPERTIES));
}
}

} catch (IOException e) {
logger.error("I/O ERROR", e);
}
}
} else {
logger
.error(String
.format(
"Le fichier [%s] n'a pas les droits de lecture, les posts-traitements sont inutilisables",
POSTTRAITEMENT_PROPERTIES));
}

} catch (FileNotFoundException e1) {
logger.error("fichier introuvable", e1);
} finally {
if (logger.isDebugEnabled()) {
Date fin = new Date();
logger.debug(String.format(
"temps pour la lecture du fichier [%d] ms", fin
.getTime()
- debut.getTime()));
}

if (r != null) {
try {
r.close();
} catch (IOException e) {
logger.error("Echec de la fermeture du fichier", e);
}
}
}

return instance;
}

public Collection getActionList() {
return Collections.unmodifiableCollection(this.actionMap.values());
}

public Collection getChampsPostTraite() {
return Collections.unmodifiableCollection(this.actionMap.keySet());
}

public boolean isPostTraite(final String idChamp) {
if (actionMap.containsKey(idChamp)) {
return true;
} else {
return false;
}
}

/**
* methode pour le hmtl
* @param idChamp
* @return
*/
public String isPostTraiteToStr(final String idChamp) {
return isPostTraite(idChamp) == true ? "true" : "false";
}

public String getAction(String methodName) {
if (actionMap.containsKey(methodName)) {
return actionMap.get(methodName);
} else {
return NONE;
}
}

public void setDirectory(String directory) {
this.directory = directory;
}

public String getDirectory() {
return directory;
}

/**
* permets de retrouver une methode a invoquer, et declarer dans le fichier postraitement.properties
* @param fieldId est la cle a rechercher
* @return une methode a invoquer
* @throws SecurityException si une erreur se produit
* @throws NoSuchMethodException si une erreur se produit
*/
private synchronized Method getActionMethod(final String fieldId)
throws SecurityException, NoSuchMethodException {
Method m = null;
String nomMethod = getAction(fieldId);

if (!methodCache.containsKey(nomMethod)) {
// si pas dans le chache, on la cherche, et on l'ajoute
Class c = PostTraitementHelper.class;
m = c.getDeclaredMethod(nomMethod, Object[].class);
methodCache.put(nomMethod, m);
} else {
m = methodCache.get(nomMethod);
}

return m;
}

/**
* methode permettant l'invocation du methode par reflexion
* @param fieldId ID du champ HMTL
* @param params listes des parametres necessaires à l'application de la regle metier
* @return l'objet calcule a partir de la regle d'affichage
*/
public Object invokeActionMethod(final String fieldId, Object... params) {
Method m = null;
Object result = null;

try {
m = getActionMethod(fieldId);
result = m.invoke(this, new Object[] { params });

} catch (SecurityException e) {
logger.error("Reflection ERROR", e);
} catch (NoSuchMethodException e) {
logger.error("Reflection ERROR", e);
} catch (IllegalArgumentException e) {
logger.error("Reflection ERROR", e);
} catch (IllegalAccessException e) {
logger.error("Reflection ERROR", e);
} catch (InvocationTargetException e) {
logger.error("Reflection ERROR", e);
}
return result;
}

public FieldDes affichageCRAM(Object[] params) {
// recupe des params
Object o = params[0];
FieldDes fieldToChange = null;

if (o instanceof FieldDes) {
fieldToChange = (FieldDes) o;
}

// pas de cast c'est une interface
User u = (User) params[1];

o = params[2];
Project p = null;
if (o instanceof Project) {
p = (Project) o;
}

if (!LinkManager.isAdmin(p, u)) {
// le champ sdoit etre post traite et l'utilisateur n'est pas un
// profil admin
String login = u.getLogin();

Element[] elts = fieldToChange.getElement();

for (Element element : elts) {
if (element.getValue().equals(login)
|| element.getKey().equals(login)) {
Element[] newElts = new Element[1];
newElts[0] = element;
fieldToChange.setElement(newElts);
break;
}
}
}

return fieldToChange;
}

public String affichageCRAMParUser(Object[] params) {
// recupe des params
Object o = params[0];
String html = "";
String htmlResult = "";

if (o instanceof String) {
html = (String) o;
htmlResult = html;
}

// pas de cast c'est une interface
User u = (User) params[1];

o = params[2];
Project p = null;
if (o instanceof Project) {
p = (Project) o;
}

o = params[3];
String tagName = "*";
if (o instanceof String) {
tagName = (String) o;
}

o = params[4];
String idValue = "";
if (o instanceof String) {
idValue = (String) o;
}

if (!LinkManager.isAdmin(p, u)) {
// le champ sdoit etre post traite et l'utilisateur n'est pas un
// profil admin
String login = u.getLogin();

try {
htmlResult = parseXML(tagName, idValue, login, html);
} catch (TransformerException e) {
logger.error("erreur lors de la transformation XML", e);
} catch (ParserConfigurationException e) {
logger.error("erreur lors parsing XML", e);
} catch (SAXException e) {
logger.error("erreur SAX lors du parsing XML", e);
} catch (IOException e) {
logger.error("erreur I/O lors du parsing XML", e);
}
}
else
{
if(logger.isDebugEnabled())
{
logger.debug("cas d'un user admin, donc pas de modification de la liste d'option");
}
}

return htmlResult;
}

public String parseXML(final String tagName, final String idValue,
final String optionAGarder, final String html)
throws TransformerException, ParserConfigurationException,
SAXException, IOException {

String htmlResult = html;

List aSupprimer = new ArrayList();

Document d = c.parse(new InputSource(new StringReader(html)));
NodeList selects = d.getElementsByTagName(tagName);
boolean found = false;
Node select = null;

// si on a trouve au moins un selec avec id qui matche, on sort
for (int i = 0; i < selects.getLength() && found != true; i++) {
select = selects.item(i);
// on recherche un tag de type tagName et contentant un attribut id
// avec la valeur idValue
NamedNodeMap atts = select.getAttributes();
Node att = atts.getNamedItem("id");

if (att.getNodeValue().equals(idValue)) {
// un noeud a ete trouve
NodeList options = select.getChildNodes();
for (int j = 0; j < options.getLength(); j++) {
Node option = options.item(j);
NamedNodeMap child = option.getAttributes();
Node attChild = child.getNamedItem("value");

String valeur = attChild.getNodeValue();

if (valeur != null
&& !valeur.equalsIgnoreCase(optionAGarder)) {
aSupprimer.add(option);
} else {
if (option instanceof org.w3c.dom.Element) {
if(logger.isDebugEnabled())
{
logger.debug("OPTION trouvee = " + valeur);
}
org.w3c.dom.Element e = (org.w3c.dom.Element) option;
e.setAttribute("selected", "selected");
/*
* on considere que c'est bon si et on a trouve
* un noeud select, et avec id correspondant et
* une option qui correspond au login sinon, on
* renvoie tout
*/
found = true;
}
}
}
}
}

if (found && !aSupprimer.isEmpty()) {
// si on a trouve et qu'il y a des option a supprimer
nettoyageDOM(aSupprimer, select);
// on regenere la chaine
htmlResult = xml2String(d);
}
else
{
if(logger.isDebugEnabled())
{
logger.debug("OPTION non trouvee pour l'id = [" + idValue + "] et la valeur = [" + optionAGarder + "]");
}
}

return htmlResult;
}

private String xml2String(Document d)
throws TransformerFactoryConfigurationError,
TransformerConfigurationException, TransformerException {
String htmlResult;
tf = TransformerFactory.newInstance();
DOMSource domSource = new DOMSource(d);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
transformer.transform(domSource, result);
htmlResult = writer.toString();
return htmlResult;
}

private void nettoyageDOM(List aSupprimer, Node select) {
for (Node supp : aSupprimer) {
select.removeChild(supp);
}
}

}

mercredi 16 février 2011

java EE : jstl méthode pour récupérer des variables JSTL en scriptlet

La chose important à noter, n'est pas que ce n'est ni très jolie, ni recommandé d'utiliser les scriplets.

La chose intéressantes est de récupérer une varaible JSTL dans le scriptlet pour pouvoir appeler une méthode JAVA :

<c:set var="fieldId" value="${field.id}"/>

et une récupération par la scriptlet qui va bien :
Object id = pageContext.getAttribute("fieldId");

Voici, l'exemple complet :

<td>
<select name="<c:out value="${field.id}"/>FIELD_POSTTRAITEMENT" onChange="javascript:submitRefresh();">
<c:set var="fieldId" value="${field.id}"/>
<c:set var="fieldPostTraitement" value="${field.postTraitement}"/>
<%
Object id = pageContext.getAttribute("fieldId");
Object postTr = pageContext.getAttribute("fieldPostTraitement");
boolean isPostTraite = com.generic.description.PostTraitementHelper.getInstance().isPostTraite((String) id);

if(isPostTraite==false){%>
<option

value="<c:out value="<%= com.generic.description.PostTraitementHelper.NONE %>"/>"><c:out

value="<%= com.generic.description.PostTraitementHelper.NONE %>" />
</option>
<%}else{%>
<c:forEach var="action" items="${NEW_PROJECT.actionList}">
<option value="<c:out value="${action}"/>"
<%
Object act = pageContext.getAttribute("action");
if(postTr.equals((String)act)){
%>
selected
<%}%>
><c:out value="${action}" /></option>
</c:forEach>
<%}%>
</select>
</td>

dimanche 13 février 2011

Java / J2EE : Créer simplement un projet tapestry

IL suffit d'utiliser l'archetype maven et lancer la commande suivante :

mvn -DarchetypeVersion=5.2.4 -Darchetype.interactive=false -DgroupId=com.enwoo -DarchetypeArtifactId=quickstart -Dversion=1.0-SNAPSHOT -DarchetypeGroupId=org.apache.tapestry -Dpackage=com.enwoo.followMe -DartifactId=followMe --batch-mode -DarchetypeRepository=http://tapestry.apache.org archetype:generate

lundi 7 février 2011

windows / svn : enregistrer svn comme un service

Voici comment enregistrer svn comme un service. Ceci permettra qu'il soit démarré dès que le PC fonctionne, et que vos petits camarades puissent l'utiliser :

Tout d'abord, il faut installer un SVN (en l'occurence silkSVN), et créer un repository (le mien se trouve dans D:\repoSVN\monProjet").

Tout d'abord, il faut enregistre le service avec la commande sc sous DOS :

sc create SvnServer binPath= "svnserve --service -r D:\ --log-file=D:\logSVN.txt" type= own start= auto

A noter:
Il faut scpécifier l'option --service pour svnserve. Les autres options servent à spécifier un fichier de log, et à préciser ou se trouve le root directory des repo SVN.

Attention, les chemin avec espace nécessite d'être encadré par des double-quotes, et les options nécessitent de passer un espace (bizarre ... ) ex : type= own

Puis, le démarrer en ligne de commande, ou via l'interface de gestion des services:
sc start SvnServer

Pour l'arrêter :
sc stop SvnServer

Pour le supprimer :
sc delete SvnServer

Ensuite, si le service est démarré , on peut tester avec tortoiseSVN (et le rep browser)ou autre par exemple :

svn co svn://localhost/repoSVN/monProjet D:\sources

unix / linux : synchroniser des arborescence de fichiers simplement

Il exsite un moyen simple de synchroniser des arborescences de fichiers / répertoire soit en local, mais plutôt avec un serveur distant :

Pour cela, sous cygwin et un serveur cible et un répertoire cible de destination avec des droits adéquats, vous pouvez utiliser la commande rsync :

Pour synchroniser un répertoire local->local
rsync -a /cygdrive/c/repSource /cygdrive/d/repDest

Pour synchroniser un répertoire distant (et faire les suppressions si besoin). On crée donc simplement un répertoire mirroir, et l'option delte-after permet de supprimer les fichiers supprimés de la source

rsync -av --delete-after ../WebContent/ enwoo@monServeurCible:/DATA/tmp/monMirroir

Attention, il faut un accès ssh. En effet, ce dernier est le protocole utilisé par défaut par rsync.

ici, un très bonne article sur ubuntu.fr

PS: attention pour les arbo CVS ou SVN, je conseille de faire un export, ce qui supprime l'ensemble des fichiers .svn ou .cvs ...

unix / linux : afficher la taille d'un répertoire

une simple commande toujours utile de s'en rappelr :

du -sh monRepertoire

windows : outils sympa deblouer / killer /tuer process

Voici un petit outil fort pratique pour trouver quel est le processus qui locke une ressources (ficher, etc...).

De plus se lie avec l'explorateur de fichier sous windows.

Il s'agit de unlocker

mercredi 2 février 2011

bash : script de redémarrage d'environnement

Voici, un script très simple qui permet de rechercher des chaines et si elle matche, ce dernier lance une commande.

Voici le code :

#!/bin/bash
#set -x

TOMCAT_PATH='/home/serverMiddle/@ENV@/tomcat1/logs/ccm'
ENVS=`cat /etc/motd`

for ENV in $ENVS
do
if [[ ("$ENV" == *wacd* && ( "$ENV" == *cgss* || "$ENV" == *cnav* || "$ENV" == *cram* )) ]]
then
#echo "chaine acceptee = [$ENV]"
stop_service $ENV";
sleep 5;
start_service $ENV";
sleep 5;
fi
done


Voici un exemple plus complet qui lit les fihcier de log et attend de trouver une chaine particuliere


#!/bin/bash
TOMCAT_PATH='/MIDDLE/cnav/ENV/tomcat1/logs'
ENVS=`cat /etc/motd`
NOM_FIC="`date +"%Y.%m.%d"`_SERVER.log"

DETECTION_ACD_STARTED="FIN APPEL SERVICE accesFileCounterrs"
DETECTION_TOMCAT_STARTED="FIN APPEL SERVICE accesFileCounterrs"

function isTomcatStarted()
{
TOMCAT_LOG_DIR=`echo "$1/stdout.log" | sed -e "s@ENV@$2@g"`

if [ -e "$TOMCAT_LOG_DIR" ]
then
while read line; do
case "$line" in
*"Ajp13Processor"*)
echo "Le server Tomcat [$2] est demarre => [$line]"
return 0;
break
;;
*)
;;
esac
done < $TOMCAT_LOG_DIR
else
echo "fichier [$TOMCAT_LOG_DIR] est introuvable"
fi

# cas erreur
return 1;
}

function isACDStarted()
{
WACD_LOG_DIR=`echo "$1/ccm/$NOM_FIC" | sed -e "s@ENV@$2@g"`

if [ -e "$WACD_LOG_DIR" ]
then
while read line; do
case "$line" in
*"Started"*)
echo "L'ACD [$2] est demarre => [$line]"
return 0;
break
;;
*)
;;
esac
done < $WACD_LOG_DIR
else
echo "fichier [$WACD_LOG_DIR] est introuvable"
fi
# cas erreur
return 1;
}

function AR()
{
echo "stop_service $ENV";
sleep 2
echo "start_service $ENV";
isACDStarted $TOMCAT_PATH $ENV
isACDOK=$?
isTomcatStarted $TOMCAT_PATH $ENV
isTomcatOK=$?

if [[ ("$isACDOK" -eq 1 || "$isTomcatStarted" -eq 1) ]]
then
echo "erreur lors du demarrage"
echo "isACDOK =[$isACDOK]"
echo "isTomcatOK=[$isTomcatOK]"
fi
}

for ENV in $ENVS
do
if [[ ("$ENV" == *wacd* && ( "$ENV" == *cgss* || "$ENV" == *cnav* || "$ENV" == *cram* )) ]]
then
AR $ENV
if [ "$?" -eq 0 ]
then
echo "isStarted OK"
else
echo "isStarted KO"
fi
fi
done