Posted: December 4th, 2009 | Author: Edu | Filed under: Java, Javascript, Scripting | Tags: Java, Javascript, Scripting | No Comments »
VN:F [1.9.2_1090]
Rating: 0.0/5 (0 votes cast)
Simple call / Llamada simple:
1
2
3
4
5
6
7
8
9
10
11
| try {
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("JavaScript");
//Simple call to println javascript function
engine.eval("println('Texto JavaScript')");
} catch (ScriptException ex) {
//Do something
} |
Calling a function / Llamando a una función:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| try {
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("JavaScript");
engine.eval("function amessagefunction(atext){ return 'The secret message is ' + atext;}");
Invocable invocable = (Invocable) engine;
Object object = invocable.invokeFunction("amessagefunction", "abc");
System.out.println(object);
} catch (NoSuchMethodException ex) {
//Do something
} catch (ScriptException ex) {
//Something is wrong, perhaps
} |
VN:F [1.9.2_1090]
Rating: 0.0/5 (0 votes cast)
Posted: November 29th, 2009 | Author: Edu | Filed under: Linux, Tools | Tags: Linux, Tools | No Comments »
VN:F [1.9.2_1090]
Rating: 0.0/5 (0 votes cast)
Cool terminal emulator for GNU/Linux users.
I installed on my Ubuntu box just typping:
$ sudo apt-get update
$ sudo apt-get install terminator
Looks like this:

VN:F [1.9.2_1090]
Rating: 0.0/5 (0 votes cast)
Posted: November 23rd, 2009 | Author: Edu | Filed under: Design Patterns, Java, Spring | Tags: Design Patterns, Java, Spring | No Comments »
VN:F [1.9.2_1090]
Rating: 0.0/5 (0 votes cast)
Simply a code I wrote today at my Spring Training / Simplemente un código que escribí hoy en el curso de Spring.
applicationContext.xml:
1
2
3
4
5
6
7
8
9
10
| <?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="postProcessor" class="com.sourcerebels.test.postprocessor.PostProcessor"/>
<bean id="someBusinessRule" class="com.sourcerebels.test.postprocessor.SomeBusinessRule" lazy-init="true">
<property name="message" value="someBusinessRule executed"/>
</bean>
</beans> |
Advice:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| package com.sourcerebels.test.postprocessor;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
public class TransactionAdvice implements MethodBeforeAdvice, AfterReturningAdvice {
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println("Starting Transaction");
}
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("Commit Transaction");
}
} |
BusinessRule interface:
1
2
3
4
5
6
| package com.sourcerebels.test.postprocessor;
public interface BusinessRule {
void run();
} |
SomeBusinessRule class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| package com.sourcerebels.test.postprocessor;
public class SomeBusinessRule implements BusinessRule {
private String message;
public void run() {
System.out.println(message);
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
} |
Postprocessor:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
| package com.sourcerebels.test.postprocessor;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
/**
* @author edu@sourcerebels.com
*/
public class PostProcessor implements BeanPostProcessor {
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessBeforeInitialization");
return bean;
}
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessAfterInitialization");
if (bean instanceof BusinessRule) {
System.out.println("building proxy object");
ProxyFactory proxyFactory = new ProxyFactory(bean);
proxyFactory.addInterface(BusinessRule.class);
proxyFactory.addAdvice(new TransactionAdvice());
return proxyFactory.getProxy();
}
return bean;
}
} |
Main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| package com.sourcerebels.test.postprocessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
String[] paths = { "com/sourcerebels/test/postprocessor/applicationContext.xml" };
ApplicationContext ctx = new ClassPathXmlApplicationContext(paths);
BusinessRule negocio = (BusinessRule) ctx.getBean("someBusinessRule");
negocio.run();
negocio = (BusinessRule) ctx.getBean("someOtherBusinessRule");
negocio.run();
}
} |
VN:F [1.9.2_1090]
Rating: 0.0/5 (0 votes cast)
Posted: November 22nd, 2009 | Author: Edu | Filed under: Compiler, Java, Maven | Tags: Compiler, Java, Maven | No Comments »
VN:F [1.9.2_1090]
Rating: 0.0/5 (0 votes cast)
At your pom.xml / En tu fichero pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
VN:F [1.9.2_1090]
Rating: 0.0/5 (0 votes cast)
Posted: November 21st, 2009 | Author: Edu | Filed under: Java, Maven, UrlRewriteFilter | Tags: Java, Maven, UrlRewriteFilter | No Comments »
VN:F [1.9.2_1090]
Rating: 0.0/5 (0 votes cast)
Add this Maven dependency to your pom.xml or download urlrewrite*.jar from UrlRewriteFilter Home / Añade esta dependencia Maven a tu pom.xml o descarga urlrewrite*.jar desde la home de UrlRewriteFilter.
<dependency>
<groupId>org.tuckey</groupId>
<artifactId>urlrewritefilter</artifactId>
<version>3.1.0</version>
</dependency>
Add this filter definition to your web.xml file / Añade esta definición de filtro a tu fichero web.xml:
<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Add the file urlrewrite.xml to your WEB-INF directory / Añade el fichero urlrewrite.xml a tu directorio WEB-INF:
<!DOCTYPE urlrewrite
PUBLIC "-//tuckey.org//DTD UrlRewrite 3.0//EN"
"http://tuckey.org/res/dtds/urlrewrite3.0.dtd">
<urlrewrite>
<rule>
<from>^/some/olddir/(.*)$</from>
<to type="redirect">/very/newdir/$1</to>
</rule>
<rule match-type="wildcard">
<from>/blog/archive/**</from>
<to type="redirect">/roller/history/$1</to>
</rule>
</urlrewrite>
Some useful links / Algunos enlaces útiles:
UrlRewriteFilter Download
UrlRewriteFilter Manual v3.0
VN:F [1.9.2_1090]
Rating: 0.0/5 (0 votes cast)