May 14, 2008

Loading Spring ApplicationContext in Class Path

Write a Startup Servlet and initialize it to 0 to load on application startup. Reading all ApplicationContext files from classpath to load them. The following example will explain how to load spring AppicationContext files while application startup. Following code write in Starup servlet.

private static final String[] CONTEXT_PATHS = {"/spring/applicationContext-*.xml" };
public void init(ServletConfig config) throws ServletException {
loadSpringResources(config);
}
private void loadSpringResources() {
ApplicationContext appContext = null;

// Set up the service locator factory...
BeanLocatorFactory.setApplicationContext(appContext);
// Start the different services.
}

Here Write the BeanLocatorFactory is as follows to set ApplicationContext and whenever your required.

public class BeanLocatorFactory {
private static ApplicationContext applicationContext = null;
private static final Map nameMap = Collections.synchronizedMap(new HashMap());

public static Object getService(Class interfaceClass) {
String cachedServiceName = (String) nameMap.get(interfaceClass);
if (cachedServiceName == null) {
int servicePackageLength = interfaceClass.getPackage().getName().length() + 1;
StringBuffer serviceName = new StringBuffer(interfaceClass.getName().substring(servicePackageLength));
cachedServiceName = serviceName.toString();
nameMap.put(interfaceClass, cachedServiceName);
}
return getService(cachedServiceName);
}
public static synchronized void setApplicationContext(ApplicationContext context) {
BeanLocatorFactory.applicationContext = context;
}
public static Object getService(String serviceName) {
try {
Object o = applicationContext.getBean(serviceName);
return o;
} catch (NoSuchBeanDefinitionException e) {
return null;
}
}
}

May 8, 2008

Image Cropping in java

//This is logic to crop image in java , here the code for how to crop image in java only. not this code runs completely
public void cropImage() {
byte[] bytesOut = null;
try {
int x1 = 0, y1 = 0, cw = 0, ch = 0;
HttpServletRequest request = getRequest();
String params = request.getParameter("dimensions");
String str[] = params.split(",");
HttpSession session = request.getSession();
String fname = (String) session.getAttribute("fileupload_name");
if (StringUtils.isNotBlank(str[0])) {
x1 = str[0].equals("") ? 50 : Integer.parseInt(str[0]);
y1 = str[1].equals("") ? 50 : Integer.parseInt(str[1]);
cw = str[2].equals("") ? 50 : Integer.parseInt(str[2]);
ch = str[3].equals("") ? 50 : Integer.parseInt(str[3]);
}
byte[] bytes = (byte[]) session .getAttribute("fileupload_bytes");
File tfile = new File(fname);
FileOutputStream fout = new FileOutputStream(tfile);
fout.write(bytes);
fout.close();
try{
BufferedImage bimage = ImageIO.read(tfile);
BufferedImage bi = null;
bi = bimage.getSubimage(x1, y1, cw, ch);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
String format = fname.substring(fname.lastIndexOf(".") + 1);
if (bi != null) {
ImageIO.write(bi, format, baos);
}
bytesOut = baos.toByteArray();
}catch(Exception exp){
log.info("Invalid image Width or Height");
}
} catch (Throwable th) {
ExceptionTrapSender.sendException(th);
}

}

May 6, 2008

Calling Url using java servlet

This is the java code servlet block, to ping a servlet on URL.
Here we are using URLConnection class to open a connection.

System.err.println("@@W@Calling Test Servlet:: Called");
URLConnection uc = new URL("http://www.example.com/Test").openConnection();
uc.setDoInput(false);
uc.setDoOutput(true);
uc.setUseCaches(false);
uc.setDefaultUseCaches(false);
uc.setRequestProperty("Content-Type", "application/octet-stream");
ObjectOutputStream out = new ObjectOutputStream(uc.getOutputStream());
HashMap hm = new HashMap();
hm.put("Key1", "Val1");

out.writeObject(hm);
out.flush();
out.close();