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 PersistenceContextGenerator extends ValueGenerator {
052: private static final Logger log = Logger
053: .getLogger(PersistenceContextGenerator.class.getName());
054: private static final L10N L = new L10N(
055: PersistenceContextGenerator.class);
056:
057: private String _location;
058:
059: private String _jndiName;
060: private String _unitName;
061: private PersistenceContextType _type;
062: private Map _properties;
063: private EntityManager _manager;
064:
065: PersistenceContextGenerator(String location, String jndiName,
066: String unitName, PersistenceContextType type, Map properties) {
067: _location = location;
068:
069: _jndiName = jndiName;
070: _unitName = unitName;
071: _type = type;
072: _properties = properties;
073: }
074:
075: PersistenceContextGenerator(String location, PersistenceContext pc) {
076: _location = location;
077:
078: _jndiName = pc.name();
079: _unitName = pc.unitName();
080: _type = pc.type();
081:
082: _properties = new HashMap();
083:
084: PersistenceProperty[] propertyList = pc.properties();
085: for (int i = 0; propertyList != null && i < propertyList.length; i++) {
086: PersistenceProperty prop = propertyList[i];
087:
088: _properties.put(prop.name(), prop.value());
089: }
090: }
091:
092: /**
093: * Returns the expected type
094: */
095: @Override
096: public Class getType() {
097: return EntityManager.class;
098: }
099:
100: /**
101: * Creates the value.
102: */
103: public Object create() {
104: if (_manager != null)
105: return _manager;
106:
107: AmberContainer amber = AmberContainer.getCurrent();
108:
109: EntityManager manager = amber.getPersistenceContext(_unitName);
110:
111: if (manager == null)
112: throw new ConfigException(
113: _location
114: + L
115: .l(
116: "@PersistenceContext '{0}' is an unknown unit",
117: _unitName));
118:
119: return manager;
120: }
121:
122: @Override
123: public String toString() {
124: return getClass().getSimpleName() + "[" + _jndiName + ","
125: + _unitName + "]";
126: }
127: }
|