top-image

OLDER ARTICLES

Hi,

I will expose you a solution implemented in order to:
– filter imports/resource loading in a spring context of general applicationContext.xml,
– filter resources in imports or even overloading of beans,
– test the environment and conditionally add path locations,

For our example, in an web application with a root context definition applicationContext.xml:

	<bean id="propertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:file1.properties</value>
				<value>classpath:file2.properties</value>
				<value>classpath:file3.properties</value>
			</list>
		</property>
	</bean>
	
	<import resource="classpath:huo-spring-business-services.xml" />
	<import resource="classpath:huo-spring-datasource.xml" />
	<import resource="classpath:huo-spring-security-config.xml" />
	<import resource="classpath:huo-spring-security-access.xml" />
	<import resource="classpath:huo-spring-wf.xml" />

This context is loaded in the web.xml file of web application:

	<context-param>
		<param-name>contextClass</param-name>
		<param-value>org.springframework.web.context.support.XmlWebApplicationContext</param-value>
	</context-param>

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/applicationContext.xml</param-value>
	</context-param>

In our example, we want to filter the last import in the root context:

<import resource="classpath:huo-spring-wf.xml" />

in terms of a properties in the “huo.config.wf.enable” in the properties file “file3.properties”:

huo.config.wf.enable=false

So, we have create a new class ConditionalConfigurationXmlWebApplicationContext extending the Spring standard XmlWebApplicationContext. This new class can override the “refresh()”, “getConfigLocations()” or “getResources()” methods, where it will test its environment and possibly modify the “configLocations”.

Important note: To be catched by our above class, we nedd to add “classpath:” in all imports like ‘import resource=”classpath:spring-except.xml” ‘.

For our example and needs, it would do something like:

	/**
	 * Map containing the resources do not load
	 */
	private Map<String, Boolean> locationPatternRessourceNOLoad = new HashMap<String, Boolean>();
	public ConditionalConfigurationXmlWebApplicationContext() {
		super();
		// Exemple de filtrage wf
		{
	        InputStream in = null;
			try{
				String name = "file3.properties";
		        Properties result = null;
		        ClassLoader  loader = this.getClassLoader();
		        if (loader == null){
		        	loader = ClassLoader.getSystemClassLoader ();
		        }
	            in = loader.getResourceAsStream (name);
	            if (in != null){
                    result = new Properties ();
					result.load(in); // Can throw IOException
	            }
	            //for (Iterator iterator = result.keySet().iterator(); iterator.hasNext();) {
	            //	Object key = iterator.next();
				//	Object value = result.get(key);
				//	System.out.println(key+"="+value);
	            //}
	        
	            Object val = result.get("huo.config.wf.enable");
	            if(val != null){
           			locationPatternRessourceNOLoad.put("classpath:huo-spring-wf.xml", Boolean.parseBoolean(val.toString()));
	            }

			}catch (Throwable ignore){
	        }finally{
	            if (in != null) try { in.close (); } catch (Throwable ignore) {}
	        }
		}
	}
	/**
	 * Resolve the given location pattern into Resource objects. 
	 */
	@Override
	public Resource[] getResources(String locationPattern) throws IOException{
		{
			Boolean toLoad = true;
			if(locationPatternRessourceNOLoad.containsKey(locationPattern)){
				toLoad = locationPatternRessourceNOLoad.get(locationPattern);
			}
			if(toLoad.booleanValue()){ // LOAD
				return super.getResources(locationPattern);
			}else{ // NO LOAD
				return new Resource[]{};
			}
		}		
	}
	/**
	 * Méthode appellée et retournant les fichiers paramétrés dans le paramètre "contextConfigLocation" du fichier web.xml 
	 */
	/*@Override
	public String[] getConfigLocations() {
		//locations = append(locations, "classpath*:/META-INF/config/service-module-test-only.xml");
		//setConfigLocations(locations);
		return super.getConfigLocations();
	}*/

So, I have this in my web.xml:

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<context-param>
		<param-name>contextClass</param-name>
		<!-- <param-value>org.springframework.web.context.support.XmlWebApplicationContext</param-value> -->
		<param-value>huo.ConditionalConfigurationXmlWebApplicationContext</param-value>
	</context-param>

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/applicationContext.xml</param-value>
	</context-param>

ConditionalConfigurationXmlWebApplicationContext.zip

Source: XmlWebApplicationContext

Best regards,

Huseyin

In the web based application, it is necessary to encode the incoming request and outgoing response in UTF-8 in order to have a application compliant with all characters.
It exists a http filter to set the contentType attribute including the content type and the encoding of the content writer (see Servlet specification).
The CharacterEncodingFilter class provided by Spring enables an automatic way of setting the character encoding in HttpServletRequest.

It is necessary to add a filter configuration in the web.xml file of your application. Be careful to position the filter in the first position in the list of different filters!

web.xml:

	<filter>
		<filter-name>CharacterEncodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>

	<filter-mapping>
		<filter-name>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

Hello again,

Here, I expose a simple solution to backup your developments in the remote directory \\YOUR_SERVER_or_EXTERNALDISK\backupWorkspaces\.

To do this:
– Create file “backup.dat” in your Eclipse workspace ie “R:\java\dev\eclipse_workspaces\default-3.3”:

echo off
set fileDate=DEV_%date:~0,2%%date:~3,2%%date:~8,2%.zip

move %1*.zip %1backup.old

echo.
echo Backup of sources on %1
echo.
C:\PROGRA~1\7-Zip\7z.exe a -tzip -r -xr!.metadata %1%fileDate% @backup_listfile.txt

IF %ERRORLEVEL% == 0 ( GOTO SUCCESS ) ELSE ( GOTO ERROR )
 
:SUCCESS
del %1backup.old
GOTO END
 
:ERROR
echo.
echo Error: %ERRORLEVEL%
echo.

:END

– Create file “backup_listfile.txt” in your Eclipse workspace ie “R:\java\dev \eclipse_workspaces\default-3.3”:

R:\java\dev\eclipse_workspaces\default-3.3

– Create a Windows scheduled task (“Scheduled Task”) that runs the daily BATCH.

Notes:
–  This batch uses 7zip compressor to be installed in C:\Program Files\7-Zip.
– You could add more resources to backup in the “backup_listfile.txt” file.

Sincerely,

backup.zip

Hello!

In my experience with Sencha/ExtJS4.0.2 framework, i am faced with a problem with the garbage collector which is run automatically every 30s stops JS execution in my IE8 / WINXP.

The following JS error apprears randomly:
Error occured:
Message: ‘events’ is null or not an object
Line: 10708
Char: 9
Code: 0
URI: http://localhost:8080/qe2web/extjs/ext-all-debug.js

The solution is to disable the Ext garbage collection by:
Ext.enableGarbageCollector = false;

Note: I have placed this code in:

Ext.application({
	name: 'qe2web',
	appFolder: '/',
	controllers: getControllers(),
	launch: function() {
		Ext.create('Ext.Viewport', {....} );
		initScreen();
		Ext.enableGarbageCollector = false;
	}
});
Page 53 of 53:« First« 50 51 52 53
bottom-img
Copyright ® 2012 Huseyin Ozveren. No reproduction, even partial, can be used from this site and all its contents including text, documents, images, etc.. without the express permission of the author.