Showing posts with label trick. Show all posts
Showing posts with label trick. Show all posts

Monday, February 6, 2012

force eclipse to recognize war maven projects

run maven command
mvn -Dwtpversion=2.0 compile eclipse:eclipse

then refresh the project

Tuesday, January 4, 2011

Configure web.xml to load the servlet context from another location


 <servlet>
<servlet-name>tatts</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/tatts-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

Tuesday, January 8, 2008

easily share eclipse user libraries (windows)

Use when:
1. working on a project on more that two computers (ie. work and home pc)
2. working on multiple projects that share common libraries
3. projects need to be easily passed around with minimal configuration
4. multiple developers are working on single project



========

  1. store all java libraries on a single folder. make sure each library is in its own folder.
  2. create a network share on this folder, and have it mapped to J: drive. if multiple machines are going to access the same set of libraries, have one of them act as a repository for the libraries and have the others map J: drive to it.
  3. in eclipse, goto Window>Preferences>Java>Build Path>User Libraries and create User Libraries for each entry in J:. no need to run steps 3-6 on all machines.
  4. click Export.
  5. click Select All and specify a filename for the .userlibraries file.
  6. click OK to export and save your settings.
  7. copy and distribute the .userlibraries file to all the other machines.
  8. in their User Libraries window, click Import and look for the copied .userlibraries file.
  9. click OK and save your settings.

Monday, January 7, 2008

simple properties file management

Use when:
1. a majority of classes have to each load its own set of settings.
2. properties file is located in folder outside the default classpath
3. a generic solution is needed for loading configuration files that are independent of the filesystem


==========

  1. create a new Class and have it named "ResourceManager"
  2. turn the class into a singleton by hiding the constructor and give it a method named getInstance() that returns an instance of the class
  3. create a static method that will handle file loading. name this method "getResource()" and have it accept a Class object named "name" as a parameter and have it return a Properties object
  4. pass the "name" object appended with ".properties" to the ClassLoader.getResourceAsStream() method to load a Properties file as an InputStream
  5. create a new Properties object and call its "load()" method using the InputStream from step 4

==========
Sample ResourceManager:

public final class ResourceManager {
private static ResourceManager inst;
private static Logger logger = Logger.getLogger(ResourceManager.class);

private ResourceManager(){

}

public static synchronized ResourceManager getInstance(){
if (inst==null)
inst= new ResourceManager();
return inst;
}
public Object clone() throws CloneNotSupportedException
{
throw new CloneNotSupportedException();
}

public Properties getResource(String name){
Properties r = new Properties();
try{
name=name.replace('.', '/');
r.load( ClassLoader.getSystemResourceAsStream(name+".properties") );
}catch(IOException e){
logger.error(e);
}
return r;
}
public Properties getResource(Class name){
return getResource(name.getName());
}
}



=========
The ResourceManager class does not have to be a singleton, but it gives the added bonus of being memory efficient because creating multiple instances of it is no longer necessary; it having only one usable method. It can also be modified so that file access is threaded, preventing any slowdown on particularly large files. But thats for another trick.