1 package org.bsf.remoting; 2 3 import org.bsf.commons.ejb.SessionAdapterBean; 4 import org.bsf.commons.ejb.EJBFactory; 5 6 import javax.ejb.CreateException ; 7 import java.io.InputStream ; 8 import java.io.IOException ; 9 import java.util.List ; 10 import java.util.ArrayList ; 11 import java.util.Random ; 12 13 34 public class StatelessTestBean extends SessionAdapterBean { 35 36 private static int exceptionCount = 0; 37 38 private List images = new ArrayList (); 39 private Random random = null; 40 41 45 public String getCallerPrincipal() { 46 String name = _ejbContext.getCallerPrincipal().getName(); 47 logGraphBegin("getCallerPrincipal : " + name); 48 return name; 49 } 50 51 52 56 public String upper(String word){ 57 return word.toUpperCase(); 58 } 59 60 64 public int compute(int varA, int varB, int operator) { 65 int result = 0; 66 switch (operator) { 67 case RemoteService.OPERATOR_ADD : 68 result = varA + varB; 69 break; 70 case RemoteService.OPERATOR_MINUS: 71 result = varA - varB; 72 break; 73 case RemoteService.OPERATOR_MULT: 74 result = varA * varB; 75 break; 76 } 77 return result; 78 } 79 80 81 88 public void throwsException() throws Exception { 89 throw new Exception ("Exception "+ exceptionCount++); 90 } 91 92 99 public void throwsSecurityException() { 100 throw new SecurityException ("No rights for this call"); 101 } 102 103 104 109 public StatelessTest createOtherBean(){ 110 return (StatelessTest) getEJBContext().getEJBObject(); 111 } 112 113 114 120 public StatefulTest createStateful(){ 121 StatefulTest result = null; 122 123 try { 124 result = ((StatefulTestHome) 125 EJBFactory.getHome(StatefulTestHome.class)).create(); 126 } catch (Exception e) { 127 throw new RuntimeException (e.getLocalizedMessage()); 128 } 129 return result; 130 } 131 132 133 139 public BSFImage getBSFImage(){ 140 int i = random.nextInt(images.size()); 141 return (BSFImage) images.get(i); 142 } 143 144 private byte[] loadImageData(String resourceName){ 145 byte[] buffer = new byte[1024*100]; 147 byte[] data = null; 148 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 149 InputStream is = loader.getResourceAsStream(resourceName); 150 int i = 0; 151 while(true){ 152 int nextByte = 0; 153 try { 154 nextByte = is.read(); 155 } catch (IOException e) { 156 throw new RuntimeException (e.getLocalizedMessage()); 157 } 158 if (nextByte == -1){ 159 data = new byte[i]; 160 System.arraycopy(buffer, 0, data, 0, i); 161 break; 162 } 163 buffer[i] = (byte)nextByte; 164 i++; 165 } 166 return data; 167 } 168 169 170 173 public void ejbCreate() throws CreateException { 174 BSFImage newImage =new BSFImage("BSF Architecture Team", 175 loadImageData("images/TeamImage.jpg")); 176 177 images.add(newImage); 178 179 newImage = new BSFImage("Our President", 180 loadImageData("images/Hendrix7.png")); 181 images.add(newImage); 182 183 newImage = new BSFImage("Our GUI Expert", 184 loadImageData("images/keithRichards.jpg")); 185 images.add(newImage); 186 187 random = new Random (); 188 189 } 190 191 } 192 | Popular Tags |