001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.superbiz;
017:
018: import javax.ejb.Init;
019: import javax.ejb.Remove;
020: import javax.ejb.Stateful;
021: import javax.ejb.Remote;
022: import javax.ejb.Local;
023: import javax.ejb.RemoteHome;
024: import javax.ejb.LocalHome;
025: import java.text.MessageFormat;
026: import java.util.HashMap;
027: import java.util.Locale;
028: import java.util.Properties;
029:
030: /**
031: * This is an EJB 3 style pojo stateful session bean
032: * it does not need to implement javax.ejb.SessionBean
033: *
034: * @author <a href="mailto:david.blevins@visi.com">David Blevins</a>
035: */
036:
037: // EJB 3.0 Style business interfaces
038: // Each of these interfaces are already annotated in the classes
039: // themselves with @Remote and @Local, so annotating them here
040: // in the bean class again is not really required.
041: @Remote({FriendlyPersonRemote.class})
042: @Local({FriendlyPersonLocal.class})
043: // EJB 2.1 Style component interfaces
044: // These interfaces, however, must be annotated here in the bean class.
045: // Use of @RemoteHome in the FriendlyPersonEjbHome class itself is not allowed.
046: // Use of @LocalHome in the FriendlyPersonEjbLocalHome class itself is also not allowed.
047: @RemoteHome(FriendlyPersonEjbHome.class)
048: @LocalHome(FriendlyPersonEjbLocalHome.class)
049: @Stateful(name="FriendlyPerson")
050: public class FriendlyPersonImpl implements FriendlyPersonLocal,
051: FriendlyPersonRemote {
052:
053: private final HashMap<String, MessageFormat> greetings;
054: private final Properties languagePreferences;
055:
056: private String defaultLanguage;
057:
058: public FriendlyPersonImpl() {
059: greetings = new HashMap();
060: languagePreferences = new Properties();
061: defaultLanguage = Locale.getDefault().getLanguage();
062:
063: addGreeting("en", "Hello {0}!");
064: addGreeting("es", "Hola {0}!");
065: addGreeting("fr", "Bonjour {0}!");
066: addGreeting("pl", "Witaj {0}!");
067: }
068:
069: /**
070: * This method corresponds to the FriendlyPersonEjbHome.create() method
071: * and the FriendlyPersonEjbLocalHome.create()
072: *
073: * If you do not have an EJBHome or EJBLocalHome interface, this method
074: * can be deleted.
075: */
076: @Init
077: public void create() {
078: }
079:
080: /**
081: * This method corresponds to the following methods:
082: * - EJBObject.remove()
083: * - EJBHome.remove(ejbObject)
084: * - EJBLocalObject.remove()
085: * - EJBLocalHome.remove(ejbObject)
086: *
087: * If you do not have an EJBHome or EJBLocalHome interface, this method
088: * can be deleted.
089: */
090: @Remove
091: public void remove() {
092: }
093:
094: public String greet(String friend) {
095: String language = languagePreferences.getProperty(friend,
096: defaultLanguage);
097: return greet(language, friend);
098: }
099:
100: public String greet(String language, String friend) {
101: MessageFormat greeting = greetings.get(language);
102: if (greeting == null) {
103: Locale locale = new Locale(language);
104: return "Sorry, I don't speak "
105: + locale.getDisplayLanguage() + ".";
106: }
107:
108: return greeting.format(new Object[] { friend });
109: }
110:
111: public void addGreeting(String language, String message) {
112: greetings.put(language, new MessageFormat(message));
113: }
114:
115: public void setLanguagePreferences(String friend, String language) {
116: languagePreferences.put(friend, language);
117: }
118:
119: public String getDefaultLanguage() {
120: return defaultLanguage;
121: }
122:
123: public void setDefaultLanguage(String defaultLanguage) {
124: this.defaultLanguage = defaultLanguage;
125: }
126: }
|