1:package is346;
   2:
   3:import javax.naming.*;
   4:import javax.rmi.PortableRemoteObject;
   5:import java.lang.Integer;
   6:import javax.naming.InitialContext;
   7:import java.util.Hashtable;
   8:import javax.ejb.EJBHome;
   9:
  10:public class TrackSessionFacadeTestClient1 {
  11:  private static final String ERROR_NULL_REMOTE = "Remote interface reference is null.  It must be created by calling one of the Home interface methods first.";
  12:  private static final int MAX_OUTPUT_LINE_LENGTH = 100;
  13:  private boolean logging = true;
  14:  private TrackSessionFacade trackSessionFacade = null;
  15:  private TrackSessionFacadeHome trackSessionFacadeHome = null;
  16:
  17:  //Construct the EJB test client
  18:  public TrackSessionFacadeTestClient1() {
  19:    initialize();
  20:    create();
  21:  }
  22:
  23:  public void initialize() {
  24:    long startTime = 0;
  25:    if (logging) {
  26:      log("Initializing bean access.");
  27:      startTime = System.currentTimeMillis();
  28:    }
  29:
  30:    try {
  31:
  32:      //get naming context
  33:      Context context = getInitialContext();
  34:      //look up jndi name
  35:      Object ref = context.lookup("TrackSessionFacade");
  36:      //look up jndi name and cast to Home interface
  37:      trackSessionFacadeHome = (TrackSessionFacadeHome) PortableRemoteObject.
  38:          narrow(ref, TrackSessionFacadeHome.class);
  39:      if (logging) {
  40:        log("Succeeded initializing bean access through Home interface.");
  41:        long endTime = System.currentTimeMillis();
  42:        log("Execution time: " + (endTime - startTime) + " ms.");
  43:      }
  44:
  45:    }
  46:    catch (Exception e) {
  47:      if (logging) {
  48:        log("Failed initializing bean access.");
  49:      }
  50:      e.printStackTrace();
  51:    }
  52:
  53:  }
  54:
  55:  private Context getInitialContext() throws NamingException {
  56:    Hashtable environment = new Hashtable();
  57:
  58:    environment.put(Context.INITIAL_CONTEXT_FACTORY,
  59:                    "org.jnp.interfaces.NamingContextFactory");
  60:    environment.put(Context.URL_PKG_PREFIXES,
  61:                    "org.jboss.naming:org.jnp.interfaces");
  62:    environment.put(Context.PROVIDER_URL, "jnp://localhost:1099");
  63:
  64:    return new InitialContext(environment);
  65:  }
  66:
  67:  //----------------------------------------------------------------------------
  68:  // Methods that use Home interface methods to generate a Remote interface reference
  69:  //----------------------------------------------------------------------------
  70:
  71:  public TrackSessionFacade create() {
  72:    long startTime = 0;
  73:    if (logging) {
  74:      log("Calling create()");
  75:      startTime = System.currentTimeMillis();
  76:    }
  77:
  78:    try {
  79:      trackSessionFacade = trackSessionFacadeHome.create();
  80:      TrackDto trackdto=new TrackDto();
  81:
  82:
  83:      trackdto.setTitle("Rock and Roll");
  84:
  85:
  86:
  87:      ArtistDto artistdto=new ArtistDto();
  88:      artistdto.setName("U2");
  89:
  90:
  91:      ArtistDto artistdto2=new ArtistDto();
  92:      artistdto2.setName("Aerosmith");
  93:
  94:
  95:      ServiceLocator servicelocator=ServiceLocator.getInstance();
  96:      ArtistSessionFacadeHome artistSessionFacadeHome=
  97:          (ArtistSessionFacadeHome) servicelocator.getEjbHome("ArtistSessionFacade", ArtistSessionFacadeHome.class);
  98:      ArtistSessionFacade artistSessionFacade=artistSessionFacadeHome.create();
  99:      artistdto.setId(artistSessionFacade.createArtist(artistdto));
 100:      artistdto2.setId(artistSessionFacade.createArtist(artistdto2));
 101:
 102:
 103:
 104:      ArtistDto[] artists=new ArtistDto[2];
 105:      artists[0]=artistdto;
 106:      artists[1]=artistdto2;
 107:      trackdto.setArtists(artists);
 108:
 109:
 110:
 111:
 112:      trackSessionFacade.createTrack(trackdto);
 113:
 114:
 115:      if (logging) {
 116:        log("Succeeded: create()");
 117:        long endTime = System.currentTimeMillis();
 118:        log("Execution time: " + (endTime - startTime) + " ms.");
 119:      }
 120:    }
 121:    catch (Exception e) {
 122:      if (logging) {
 123:        log("Failed : create()");
 124:      }
 125:      e.printStackTrace();
 126:    }
 127:    if (logging) {
 128:      log("Return value from create(): " + trackSessionFacade + ".");
 129:    }
 130:    return trackSessionFacade;
 131:  }
 132:
 133:  //----------------------------------------------------------------------------
 134:  // Methods that use Remote interface methods to access data through the bean
 135:  //----------------------------------------------------------------------------
 136:
 137:  public void createTrack(TrackDto trackDto) {
 138:    if (trackSessionFacade == null) {
 139:      System.out.println("Error in createTrack(): " + ERROR_NULL_REMOTE);
 140:      return;
 141:    }
 142:    long startTime = 0;
 143:    if (logging) {
 144:      log("Calling createTrack(" + trackDto + ")");
 145:      startTime = System.currentTimeMillis();
 146:    }
 147:
 148:    try {
 149:      trackSessionFacade.createTrack(trackDto);
 150:      if (logging) {
 151:        log("Succeeded: createTrack(" + trackDto + ")");
 152:        long endTime = System.currentTimeMillis();
 153:        log("Execution time: " + (endTime - startTime) + " ms.");
 154:      }
 155:    }
 156:    catch (Exception e) {
 157:      if (logging) {
 158:        log("Failed : createTrack(" + trackDto + ")");
 159:      }
 160:      e.printStackTrace();
 161:    }
 162:  }
 163:
 164:  public void removeTrack(Integer id) {
 165:    if (trackSessionFacade == null) {
 166:      System.out.println("Error in removeTrack(): " + ERROR_NULL_REMOTE);
 167:      return;
 168:    }
 169:    long startTime = 0;
 170:    if (logging) {
 171:      log("Calling removeTrack(" + id + ")");
 172:      startTime = System.currentTimeMillis();
 173:    }
 174:
 175:    try {
 176:      trackSessionFacade.removeTrack(id);
 177:      if (logging) {
 178:        log("Succeeded: removeTrack(" + id + ")");
 179:        long endTime = System.currentTimeMillis();
 180:        log("Execution time: " + (endTime - startTime) + " ms.");
 181:      }
 182:    }
 183:    catch (Exception e) {
 184:      if (logging) {
 185:        log("Failed : removeTrack(" + id + ")");
 186:      }
 187:      e.printStackTrace();
 188:    }
 189:  }
 190:
 191:  public void removeTrack(TrackDto trackDto) {
 192:    if (trackSessionFacade == null) {
 193:      System.out.println("Error in removeTrack(): " + ERROR_NULL_REMOTE);
 194:      return;
 195:    }
 196:    long startTime = 0;
 197:    if (logging) {
 198:      log("Calling removeTrack(" + trackDto + ")");
 199:      startTime = System.currentTimeMillis();
 200:    }
 201:
 202:    try {
 203:      trackSessionFacade.removeTrack(trackDto);
 204:      if (logging) {
 205:        log("Succeeded: removeTrack(" + trackDto + ")");
 206:        long endTime = System.currentTimeMillis();
 207:        log("Execution time: " + (endTime - startTime) + " ms.");
 208:      }
 209:    }
 210:    catch (Exception e) {
 211:      if (logging) {
 212:        log("Failed : removeTrack(" + trackDto + ")");
 213:      }
 214:      e.printStackTrace();
 215:    }
 216:  }
 217:
 218:  public void updateTrack(TrackDto trackDto) {
 219:    if (trackSessionFacade == null) {
 220:      System.out.println("Error in updateTrack(): " + ERROR_NULL_REMOTE);
 221:      return;
 222:    }
 223:    long startTime = 0;
 224:    if (logging) {
 225:      log("Calling updateTrack(" + trackDto + ")");
 226:      startTime = System.currentTimeMillis();
 227:    }
 228:
 229:    try {
 230:      trackSessionFacade.updateTrack(trackDto);
 231:      if (logging) {
 232:        log("Succeeded: updateTrack(" + trackDto + ")");
 233:        long endTime = System.currentTimeMillis();
 234:        log("Execution time: " + (endTime - startTime) + " ms.");
 235:      }
 236:    }
 237:    catch (Exception e) {
 238:      if (logging) {
 239:        log("Failed : updateTrack(" + trackDto + ")");
 240:      }
 241:      e.printStackTrace();
 242:    }
 243:  }
 244:
 245:  public void updateTracks(TrackDto[] trackDtos) {
 246:    if (trackSessionFacade == null) {
 247:      System.out.println("Error in updateTracks(): " + ERROR_NULL_REMOTE);
 248:      return;
 249:    }
 250:    long startTime = 0;
 251:    if (logging) {
 252:      log("Calling updateTracks(" + trackDtos + ")");
 253:      startTime = System.currentTimeMillis();
 254:    }
 255:
 256:    try {
 257:      trackSessionFacade.updateTracks(trackDtos);
 258:      if (logging) {
 259:        log("Succeeded: updateTracks(" + trackDtos + ")");
 260:        long endTime = System.currentTimeMillis();
 261:        log("Execution time: " + (endTime - startTime) + " ms.");
 262:      }
 263:    }
 264:    catch (Exception e) {
 265:      if (logging) {
 266:        log("Failed : updateTracks(" + trackDtos + ")");
 267:      }
 268:      e.printStackTrace();
 269:    }
 270:  }
 271:
 272:  public TrackDto trackFindByPrimaryKey(Integer id) {
 273:    TrackDto returnValue = null;
 274:    if (trackSessionFacade == null) {
 275:      System.out.println("Error in trackFindByPrimaryKey(): " +
 276:                         ERROR_NULL_REMOTE);
 277:      return returnValue;
 278:    }
 279:    long startTime = 0;
 280:    if (logging) {
 281:      log("Calling trackFindByPrimaryKey(" + id + ")");
 282:      startTime = System.currentTimeMillis();
 283:    }
 284:
 285:    try {
 286:      returnValue = trackSessionFacade.trackFindByPrimaryKey(id);
 287:      if (logging) {
 288:        log("Succeeded: trackFindByPrimaryKey(" + id + ")");
 289:        long endTime = System.currentTimeMillis();
 290:        log("Execution time: " + (endTime - startTime) + " ms.");
 291:      }
 292:    }
 293:    catch (Exception e) {
 294:      if (logging) {
 295:        log("Failed : trackFindByPrimaryKey(" + id + ")");
 296:      }
 297:      e.printStackTrace();
 298:    }
 299:    if (logging) {
 300:      log("Return value from trackFindByPrimaryKey(" + id + "): " + returnValue +
 301:          ".");
 302:    }
 303:    return returnValue;
 304:  }
 305:
 306:  //----------------------------------------------------------------------------
 307:  // Utility Methods
 308:  //----------------------------------------------------------------------------
 309:
 310:  private void log(String message) {
 311:
 312:    if (message == null) {
 313:      System.out.println("-- null");
 314:    }
 315:    if (message.length() > MAX_OUTPUT_LINE_LENGTH) {
 316:      System.out.println("-- " + message.substring(0, MAX_OUTPUT_LINE_LENGTH) +
 317:                         " ...");
 318:    }
 319:    else {
 320:      System.out.println("-- " + message);
 321:    }
 322:  }
 323:
 324:  //Main method
 325:  public static void main(String[] args) {
 326:    TrackSessionFacadeTestClient1 client = new TrackSessionFacadeTestClient1();
 327:    // Use the client object to call one of the Home interface wrappers
 328:    // above, to create a Remote interface reference to the bean.
 329:    // If the return value is of the Remote interface type, you can use it
 330:    // to access the remote interface methods.  You can also just use the
 331:    // client object to call the Remote interface wrappers.
 332:  }
 333:}