⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 taservice.java

📁 一个web service的实例
💻 JAVA
字号:
package com.ibm.ta.webservice;

import java.rmi.RemoteException;
import javax.servlet.ServletContext;
import javax.transaction.SystemException;
import javax.transaction.UserTransaction;
import javax.xml.rpc.ServiceException;
import javax.xml.rpc.server.ServiceLifecycle;
import javax.xml.rpc.server.ServletEndpointContext;

import com.ibm.ta.dao.CourseDAO;
import com.ibm.ta.dao.DAOFactory;
import com.ibm.ta.dao.EnrollmentDAO;
import com.ibm.ta.dao.StudentDAO;

public class TAService implements TAService_SEI, ServiceLifecycle {
	private DBConfig dbConfig;

	private ServletContext servletContext;

	public void init(Object arg) throws ServiceException {
		String resRef = null;
		int dbType = 0;

		servletContext = ((ServletEndpointContext) arg).getServletContext();

		// Get and check context parameters
		resRef = servletContext.getInitParameter("resref");
		if (resRef == null || resRef.trim().length() == 0) {
			throw new ServiceException("Parameter resref is null or invalid");
		}

		try {
			dbType = Integer
					.parseInt(servletContext.getInitParameter("dbtype"));
		} catch (NumberFormatException nfe) {
			throw new ServiceException("Parameter dbtype is not numeric");
		}

		if (dbType != DBConfig.DB2 && dbType != DBConfig.MYSQL) {
			throw new ServiceException(
					"Parameter dbtype has to be 1 (DB2) or 2 (MySQL)");
		}

		// Save dbConfig as instance variable
		dbConfig = new DBConfig(resRef, dbType);

		try {
			ServiceLocator.initializeInstance();
		} catch (TAServiceException se) {
			servletContext.log(se.getMessage(), se);
			throw new ServiceException(se.getMessage());
		}
	}

	public void destroy() {
		// Nothing to do
	}

	public Course[] listCourses() throws RemoteException, TAServiceException {
		Course[] courses = null;
		UserTransaction ut = null;

		// Access the database within a transaction
		try {
			ut = ServiceLocator.getInstance().getUserTransaction();
			ut.begin();

			DAOFactory daoFactory = DAOFactory.getDAOFactory(dbConfig);
			CourseDAO courseDAO = daoFactory.getCourseDAO();
			courses = courseDAO.selectCourses();

			// End the transaction
			ut.commit();
		} catch (Exception e) {
			try {
				if (ut != null) {
					ut.rollback();
				}
			} catch (SystemException se) {
				// Only throw the first failure exception
			}
			servletContext.log(e.getMessage(), e);
			throw new TAServiceException(e.getMessage(), e);
		}

		return courses;
	}

	public Student[] listStudents() throws RemoteException, TAServiceException {
		Student[] students = null;
		UserTransaction ut = null;

		// Access the database within a transaction
		try {
			ut = ServiceLocator.getInstance().getUserTransaction();
			ut.begin();
			DAOFactory daoFactory = DAOFactory.getDAOFactory(dbConfig);
			StudentDAO studentDAO = daoFactory.getStudentDAO();
			students = studentDAO.selectStudents();

			// End the transaction
			ut.commit();
		} catch (Exception e) {
			try {
				if (ut != null) {
					ut.rollback();
				}
			} catch (SystemException se) {
				// Only throw the first failure exception
			}
			servletContext.log(e.getMessage(), e);
			throw new TAServiceException(e.getMessage(), e);
		}

		return students;
	}

	public void enroll(long studentID, long courseID) throws RemoteException,
			TAServiceException {
		UserTransaction ut = null;

		// Access the database within a transaction
		try {
			ut = ServiceLocator.getInstance().getUserTransaction();
			ut.begin();

			DAOFactory daoFactory = DAOFactory.getDAOFactory(dbConfig);
			EnrollmentDAO enrollmentDAO = daoFactory.getEnrollmentDAO();
			enrollmentDAO.insertEnrollment(studentID, courseID);

			// End the transaction
			ut.commit();
		} catch (Exception e) {
			try {
				if (ut != null) {
					ut.rollback();
				}
			} catch (SystemException se) {
				// Only throw the first failure exception
			}
			servletContext.log(e.getMessage(), e);
			throw new TAServiceException(e.getMessage(), e);
		}

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -