JBOSS EJB WEB SERVICES Example

Employee Service.java
---------------------------------
package com.crm.services;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import com.crm.bean.Employee;
/**
* @author AnilKumar
*
*/
@WebService
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface EmployeeService {
public Employee getEmployee(long pin);
}

EmployeeServiceImpl.java
-------------------------------------
package com.crm.services;

import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;

import com.crm.bean.Employee;
import com.crm.model.EmployeeDAO;
import com.crm.modelinterface.cmsEmployee;
/**
* @author AnilKumar
*
*/
@Stateless
@WebService(endpointInterface = "com.crm.services.EmployeeService")
@SOAPBinding(style = SOAPBinding.Style.RPC)
public class EmployeeServiceImpl {

@WebMethod(operationName = "getEmployee")
public Employee getEmployee(@WebParam(name = "pin")
long pin) {
cmsEmployee employee2 = new EmployeeDAO();
// Employee employee2 = employeeDAO.getEmployeebyemployee(pin);

Employee employee = employee2.getEmployeebyemployee(pin);

System.out.println(employee.getFirstname());
return employee;
}

}

deploy above two classes by archieving in a jar and copy it to deploy folder of jboss
then run ur jboss server with url http://localhost:8080/jbossws there u will get all ur
web services running in your server

the client program will be like this

package com.crm.serviceClient;

import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;

import com.crm.bean.Employee;
import com.crm.services.EmployeeService;
/**
* @author AnilKumar
*
*/
@WebServiceClient(name = "EmployeeServiceImplService", targetNamespace = "http://services.crm.com/",
wsdlLocation = "http://localhost:8080/jarfilename/EmployeeServiceImpl?wsdl")
public class EmployeeServiceClient extends Service {
protected EmployeeServiceClient(URL wsdlDocumentLocation,
QName serviceName) {
super(wsdlDocumentLocation, serviceName);
// TODO Auto-generated constructor stub
}

public EmployeeServiceClient() throws MalformedURLException {
super(new URL(
"http://localhost:8080/jarfilename/EmployeeServiceImpl?wsdl"),
new QName("http://services.crm.com/",
"EmployeeServiceImplService"));
}

@WebEndpoint
public EmployeeService getEmployeeServiceport() {
return (EmployeeService) super.getPort(new QName(
"http://services.crm.com/", "EmployeeServiceImplPort"),
EmployeeService.class);

}

public static void main(String[] args) throws MalformedURLException {

try {
EmployeeService service = new EmployeeServiceClient().getEmployeeServiceport();

if (service != null) {
System.out.println("Serrvice not null");
Employee employee1 = service.getEmployee(18);

System.out.println(employee1.getFirstname());
System.out.println(employee1.getLastname());
}

} catch (Exception e) {
e.printStackTrace();
}

}

}

How to Improve Your Memory & Exercise Your Brain

How to Improve Your Memory & Exercise Your Brain
Everyone can take steps to improve their memory, with time and practice most people can gain the ability to memorize seemingly impossible amounts of information. Whether you want to win the World Memory Championships, ace your history test, or simply remember where you put your keys, this article can get you started. Scientists believe that exercising your brain can create a `cognitive reserve' that will help you stay sharp as you age.

1. Convince yourself that you do have a good memory that will improve. Too many people get stuck here and convince themselves that their memory is bad, that they are just not good with names, that numbers just slip out of their minds for some reason. Erase those thoughts and vow to improve your memory. Commit yourself to the task and bask in your achievements it's hard to keep motivated if you beat yourself down every time you make a little bit of progress.

2. Keep your brain active. The brain is not a muscle, but regularly "exercising" the brain actually does keep it growing and spurs the development of new nerve connections that can help improve memory. By developing new mental skills especially complex ones such as learning a new language or learning to play a new musical instrument and challenging your brain with puzzles and games you can keep your brain active and improve its physiological functioning.

3. Exercise daily. Regular aerobic exercise improves circulation and efficiency throughout the body, including in the brain, and can help ward off the memory loss that comes with aging. Exercise also makes you more alert and relaxed, and can thereby improve your memory uptake, allowing you to take better mental "pictures."

4. Reduce stress. Chronic stress, although it does not physically damage the brain, can make remembering much more difficult. Even temporary stresses can make it more difficult to effectively focus on concepts and observe things. Try to relax, regularly practice yoga or other stretching exercises, and see a doctor if you have severe chronic stress.

5. Eat well and eat right. There are a lot of herbal supplements on the market that claim to improve memory, but none have yet been shown to be effective in clinical tests (although small studies have shown some promising results for ginkgo biloba and phosphatidylserine). A healthy diet, however, contributes to a healthy brain, and foods containing antioxidants broccoli, blueberries, spinach, and berries, for example and Omega-3 fatty acids appear to promote healthy brain functioning. Feed your brain with such supplements as Thiamine, Vitamin E, Niacin and Vitamin B-6. Grazing, eating 5 or 6 small meals throughout the day instead of 3 large meals, also seems to improve mental functioning (including memory) by limiting dips in blood sugar, which may negatively affect the brain.

6. Take better pictures. Often we forget things not because our memory is bad, but rather because our observational skills need work. One common situation where this occurs (and which almost everyone can relate to) is meeting new people. Often we don't really learn people's names at first because we aren't really concentrating on remembering them. You'll find that if you make a conscious effort to remember such things, you'll do much better. One way to train yourself to be more observant is to look at an unfamiliar photograph for a few seconds and then turn the photograph over and describe or write down as many details as you can about the photograph. Try closing your eyes and picturing the photo in your mind. Use a new photograph each time you try this exercise, and with regular practice you will find you're able to remember more details with even shorter glimpses of the photos.

7. Give yourself time to form a memory. Memories are very fragile in the short-term, and distractions can make you quickly forget something as simple as a phone number. The key to avoid losing memories before you can even form them is to be able to focus on the thing to be remembered for a while without thinking about other things, so when you're trying to remember something, avoid distractions and complicated tasks for a few minutes.

8. Create vivid, memorable images. You remember information more easily if you can visualize it. If you want to associate a child with a book, try not to visualize the child reading the book that's too simple and forgettable. Instead, come up with something more jarring, something that sticks, like the book chasing the child, or the child eating the book. It's your mind make the images as shocking and emotional as possible to keep the associations strong.

9. Repeat things you need to learn. The more times you hear, see, or think about something, the more surely you'll remember it, right? It's a no-brainer. When you want to remember something, be it your new coworker's name or your best friend's birthday, repeat it, either out loud or silently. Try writing it down; think about it.

10. Group things you need to remember. Random lists of things (a shopping list, for example) can be especially difficult to remember. To make it easier, try categorizing the individual things from the list. If you can remember that, among other things, you wanted to buy four different kinds of vegetables, you'll find it easier to remember all four.

11. Organize your life. Keep items that you frequently need, such as keys and eyeglasses, in the same place every time. Use an electronic organizer or daily planner to keep track of appointments, due dates for bills, and other tasks. Keep phone numbers and addresses in an address book or enter them into your computer or cell phone. Improved organization can help free up your powers of concentration so that you can remember less routine things. Even if being organized doesn't improve your memory, you'll receive a lot of the same benefits (i.e. you won't have to search for your keys anymore).

12. Try meditation. Research now suggests that people who regularly practice "mindfulness" meditation are able to focus better and may have better memories. Mindfulness (also known as awareness or insight meditation) is the type commonly practiced in Western countries and is easy to learn. Studies at Massachusetts General Hospital show that regular meditation thickens the cerebral cortex in the brain by increasing the blood flow to that region. Some researchers believe this can enhance attention span, focus, and memory.

13. Sleep well. The amount of sleep we get affects the brain's ability to recall recently learned information. Getting a good night's sleep a minimum of seven hours a night may improve your short-term memory and long-term relational memory, according to recent studies conducted at the Harvard Medical School.
14. Build your memorization arsenal. Learn pegs, memory palaces, and the Dominic System. These techniques form the foundation for mnemonic techniques, and will visibly improve your memory.

15. Venture out and learn from your mistakes. Go ahead and take a stab at memorizing the first one hundred digits of pi, or, if you've done that already, the first one thousand. Memorize the monarchs of England through your memory palaces, or your grocery list through visualization. Through diligent effort you will eventually master the
art of memorization.

MBean Example in JBoss

A Hello World Service
package com.acme;


import org.jboss.system.ServiceMBean;

public interface HelloWorldServiceMBean extends ServiceMBean
{
// Configure getters and setters for the message attribute
String getMessage();
void setMessage(String message);

// The print message operation
void printMessage();
}



package com.acme;


import org.jboss.system.ServiceMBeanSupport;

public class HelloWorldService extends ServiceMBeanSupport implements HelloWorldServiceMBean
{
// Our message attribute
private String message = "Sorry no message today";

// Getters and Setters
public String getMessage()
{
return message;
}
public void setMessage(String message)
{
this.message = message;
}

// The printMessage operation
public void printMessage()
{
log.info(message);
}

// The lifecycle
protected void startService() throws Exception
{
log.info("Starting with message=" + message);
}
protected void stopService() throws Exception
{
log.info("Stopping with message=" + message);
}
}

jboss-service.xml

<?xml?>

<server>
<mbean code="com.acme.HelloWorldService" name="acme.com:service=HelloWorld">
<attribute name="Message">Hello World</attribute>
</mbean>
</server>


Sar File directory structure
hello-world.sar

hello-world.sar/META-INF/jboss-service.xml
hello-world.sar/com/acme/HelloWorldService.class
hello-world.sar/com/acme/HelloWorldServiceMBean.class






open Office to pdf conversion and document processing

package com.documentProcessing;

import java.io.Serializable;

public class FindReplace implements Serializable{
String find;
String replace;


public FindReplace() {
// TODO Auto-generated constructor stub
}
public FindReplace(String bookmark, String fieldName) {
this.find = bookmark;
this.replace = fieldName;
}

public String getFind() {
return find;
}

public void setFind(String find) {
this.find = find;
}

public String getReplace() {
return replace;
}

public void setReplace(String replace) {
this.replace = replace;
}
}




package com.documentProcessing;


import java.io.Serializable;
import java.util.List;

import com.sun.star.frame.XDesktop;
import com.sun.star.text.XTextDocument;

/**
* Class created to help the document manipulation.
*
* @author Marco Silva
*
*/
public class OpenOffice implements Serializable {
// Open Office Controllers
public XDesktop xDesktop;
public XTextDocument xTextDocument;

// Process Document
public String libraryDocumentName;
public String documentName;
public List findReplaceList;

public String getDocumentName() {
return documentName;
}
public void setDocumentName(String documentName) {
this.documentName = documentName;
}

public List getFindReplaceList() {
return findReplaceList;
}
public void setFindReplaceList(List findReplaceList) {
this.findReplaceList = findReplaceList;
}

public String getLibraryDocumentName() {
return libraryDocumentName;
}
public void setLibraryDocumentName(String libraryDocumentName) {
this.libraryDocumentName = libraryDocumentName;
}

public XDesktop getXDesktop() {
return xDesktop;
}
public void setXDesktop(XDesktop desktop) {
xDesktop = desktop;
}

public XTextDocument getXTextDocument() {
return xTextDocument;
}
public void setXTextDocument(XTextDocument textDocument) {
xTextDocument = textDocument;
}

public void closeDocument() {
try {
if (xTextDocument != null) xTextDocument.dispose();
if (xDesktop != null) xDesktop.terminate();
} catch (Exception e) {}
}
}



package com.documentProcessing;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.ejb.Stateless;

import ooo.connector.BootstrapSocketConnector;

import com.sun.star.beans.PropertyValue;
import com.sun.star.beans.XPropertySet;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.frame.XDesktop;
import com.sun.star.frame.XStorable;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.text.XTextDocument;
import com.sun.star.text.XTextRange;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;
import com.sun.star.uno.XInterface;
import com.sun.star.util.XSearchDescriptor;
import com.sun.star.util.XSearchable;

public class OpenOfficeManager {
OpenOffice oofice;

/**
* Create the OpenOffice Document Controller
*
* @return OpenOffice
*/
public void openOfficeDocument(String docLibrary, String docName,
List findReplace) {
oofice = new OpenOffice();
XMultiComponentFactory xMCF = null;

try {
// TODO: Make sure the location of the open office is correct
// String sOffice = System.getProperty( "os.name" ).startsWith(
// "Windows" ) ? "C:\\Program Files\\OpenOffice.org 2.0\\program" :
// "//home//eanilkumar//openoffice//usr//lib//openoffice//program";

String sOffice = "/opt/openoffice.org2.3/program";

// Bootstrap open office with the location of the OpenOffice
// executable file
/*
* URL[] jarList = new URL[] { new URL("file://" + sOffice) };
* URLClassLoader loader = new URLClassLoader(jarList);
*/

XComponentContext xContext = BootstrapSocketConnector
.bootstrap(sOffice);

// Get the remote office service manager
xMCF = xContext.getServiceManager();
if (xMCF != null) {
Object oDesktop = xMCF.createInstanceWithContext(
"com.sun.star.frame.Desktop", xContext);
oofice.xDesktop = (XDesktop) UnoRuntime.queryInterface(
XDesktop.class, oDesktop);

} else
System.out
.println("Can't create a desktop. No connection, no remote office servicemanager available!");

// Update documents names
oofice.documentName = docName;
oofice.libraryDocumentName = docLibrary;
oofice.findReplaceList = findReplace;

openTextdocument();

} catch (Exception e) {
System.out.println("Exception in Document opening");
e.printStackTrace();
}
}

/**
*
*/
public void closeDocument() {
oofice.closeDocument();
}

/**
* Open the document library
*
* @param OpenOffice
* oofice
*/
public void openTextdocument() {
try {

XComponent xComponent = openDocument(oofice.xDesktop,
oofice.libraryDocumentName + oofice.documentName);
oofice.xTextDocument = (XTextDocument) UnoRuntime.queryInterface(
XTextDocument.class, xComponent);
} catch (Exception e) {
e.printStackTrace(System.err);
}
}

/**
* @param XDesktop
* xDesktop - Open Office Desktop, used to open the document
* @param sURL -
* location of the document
*
* @return XComponent - the component - internal open office document
* controller
*/
protected XComponent openDocument(XDesktop xDesktop, String sURL) {
XComponent xComponent = null;
XComponentLoader xComponentLoader = null;

// Open the document in hidden mode, will not display the document
PropertyValue xArgs[] = new PropertyValue[1];
xArgs[0] = new PropertyValue();
xArgs[0].Name = "Hidden";
xArgs[0].Value = new Boolean(true);

try {
xComponentLoader = (XComponentLoader) UnoRuntime.queryInterface(
XComponentLoader.class, xDesktop);

// xComponent =
// xComponentLoader.loadComponentFromURL("private:factory/scalc",
// "_blank", 0, xArgs);
xComponent = xComponentLoader.loadComponentFromURL(sURL, "_blank",
0, xArgs);

// xComponent = xComponentLoader.loadComponentFromURL("", "_blank",
// 0, xArgs);
} catch (Exception e) {
e.printStackTrace();
}
return xComponent;
}

/**
* Save the document as PDF
*
* @param OpenOffice
* oofice
*/
public void savePDFDocument() {
PropertyValue xValues[] = new PropertyValue[2];
xValues[0] = new PropertyValue();
xValues[0].Name = "CompressMode";
xValues[0].Value = "0";
xValues[1] = new PropertyValue();
xValues[1].Name = "FilterName";
xValues[1].Value = "writer_pdf_Export";
XStorable xStorable = (XStorable) UnoRuntime.queryInterface(
XStorable.class, oofice.xTextDocument);
try {

System.out.println("Pdf Dpcument to save path"
+ oofice.libraryDocumentName + oofice.documentName);
xStorable.storeToURL(oofice.libraryDocumentName + "tmp//"
+ oofice.documentName + ".pdf", xValues);
} catch (Exception e) {
e.printStackTrace();
}
}

public String GetDocumentText() {
return oofice.xTextDocument.getText().getString();
}

/**
* Find all specific string in the document and replace for another string.
* Used in merge database information in the document
*
* @param OpenOffice
* oofice
*/
public void replaceDocument(Map record) {

if (oofice.findReplaceList == null)
return;

Iterator ite = oofice.findReplaceList.iterator();

while (ite.hasNext()) {

// Map findReplace = (Map) ite.next();

FindReplace findReplace = (FindReplace) ite.next();

// String value = (String) record.get((String)
// findReplace.get("library_field_field"));
// String bookmark = (String)
// findReplace.get("library_field_bookmark");

String value = (String) record.get((String) findReplace
.getReplace());

String bookmark = (String) findReplace.getFind();

if ((value != null) && (bookmark != null))
replaceDocumentText(oofice.xTextDocument, bookmark, value);
}
}

/**
* @param XTextDocument
* xTextDocument - Open Office Document
* @param String
* find - string to find in the document
* @param String
* replace - string to replace
*/
protected void replaceDocumentText(XTextDocument xTextDocument,
String find, String replace) {
XInterface xSearchInterface = null;
XTextRange xSearchTextRange = null;

try {
do {
// the findfirst returns a XInterface
xSearchInterface = (XInterface) FindFirst(xTextDocument, find);

if (xSearchInterface != null) {
// get the TextRange form the XInterface
xSearchTextRange = (XTextRange) UnoRuntime.queryInterface(
XTextRange.class, xSearchInterface);
xSearchTextRange.setString(replace);
}
} while (xSearchInterface != null);

} catch (Exception e) {
e.printStackTrace();
}
}

/**
* @param XTextDocument
* xTextDocument - Open Office Document
* @param sSearchString -
* string to search
*
* @return XInterface - position where the string are
*/
protected XInterface FindFirst(XTextDocument xTextDocument,
String sSearchString) {
XSearchDescriptor xSearchDescriptor = null;
XSearchable xSearchable = null;
XInterface xSearchInterface = null;

try {
xSearchable = (XSearchable) UnoRuntime.queryInterface(
XSearchable.class, xTextDocument);
xSearchDescriptor = (XSearchDescriptor) xSearchable
.createSearchDescriptor();
xSearchDescriptor.setSearchString(sSearchString);

XPropertySet xPropertySet = null;
xPropertySet = (XPropertySet) UnoRuntime.queryInterface(
XPropertySet.class, xSearchDescriptor);
xPropertySet.setPropertyValue("SearchRegularExpression",
new Boolean(true));

xSearchInterface = (XInterface) xSearchable
.findFirst(xSearchDescriptor);
} catch (Exception e) {
e.printStackTrace();
}
return xSearchInterface;
}

public static void main(String[] args) {

}

public void processDocument(String filePath, String fileName,
List findReplace, Map record) {

OpenOfficeManager openOfficeManager = new OpenOfficeManager();

Map record1 = new HashMap();
record.put("firstname", "Marco");
record.put("lastname", "Silva");
record.put("coordinator", "WILLIS HALE");

List fr = new ArrayList();
FindReplace opt1 = new FindReplace("<>", "firstname");
FindReplace opt2 = new FindReplace("<>", "lastname");
FindReplace opt3 = new FindReplace("<>", "coordinator");
fr.add(opt1);
fr.add(opt2);
fr.add(opt3);

openOfficeManager.openOfficeDocument("file:///home//eanilkumar//",
"Test.doc", fr);
openOfficeManager.replaceDocument(record1);
openOfficeManager.savePDFDocument();

System.out.println(openOfficeManager.GetDocumentText());
openOfficeManager.closeDocument();

}

}