001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.config.j2ee;
031:
032: import com.caucho.config.program.ValueGenerator;
033: import com.caucho.amber.manager.EntityManagerProxy;
034: import com.caucho.amber.manager.AmberContainer;
035: import com.caucho.config.program.ConfigProgram;
036: import com.caucho.config.ConfigException;
037: import com.caucho.config.ConfigContext;
038: import com.caucho.naming.*;
039: import com.caucho.util.L10N;
040:
041: import javax.naming.InitialContext;
042: import javax.naming.NamingException;
043: import javax.persistence.*;
044: import java.lang.reflect.AccessibleObject;
045: import java.lang.reflect.Field;
046: import java.lang.reflect.Method;
047: import java.util.*;
048: import java.util.logging.Level;
049: import java.util.logging.Logger;
050:
051: public class PersistenceUnitGenerator extends ValueGenerator implements
052: ObjectProxy {
053: private static final Logger log = Logger
054: .getLogger(PersistenceUnitGenerator.class.getName());
055: private static final L10N L = new L10N(
056: PersistenceUnitGenerator.class);
057:
058: private AmberContainer _amber;
059:
060: private String _location;
061: private String _jndiName;
062: private String _unitName;
063: private EntityManagerFactory _factory;
064:
065: PersistenceUnitGenerator(String location, String jndiName,
066: String unitName) {
067: _location = location;
068:
069: _jndiName = jndiName;
070: _unitName = unitName;
071: }
072:
073: PersistenceUnitGenerator(String location, PersistenceUnit unit) {
074: _location = location;
075:
076: _jndiName = unit.name();
077: _unitName = unit.unitName();
078: }
079:
080: /**
081: * Returns the expected type
082: */
083: @Override
084: public Class getType() {
085: return EntityManagerFactory.class;
086: }
087:
088: /**
089: * Creates the value.
090: */
091: public Object create() {
092: if (_amber == null)
093: _amber = AmberContainer.getCurrent();
094:
095: EntityManagerFactory factory = _amber
096: .getEntityManagerFactory(_unitName);
097:
098: if (factory == null)
099: throw new ConfigException(_location
100: + L.l("@PersistenceUnit '{0}' is an unknown unit",
101: _unitName));
102:
103: return factory;
104: }
105:
106: @Override
107: public String toString() {
108: return getClass().getSimpleName() + "[" + _jndiName + ","
109: + _unitName + "]";
110: }
111: }
|