Home » cxf-dosgi-ri-source-distribution-1.1 » org.apache.cxf.dosgi.samples.greeter.client » [javadoc | source]

    1   /** 
    2     * Licensed to the Apache Software Foundation (ASF) under one 
    3     * or more contributor license agreements. See the NOTICE file 
    4     * distributed with this work for additional information 
    5     * regarding copyright ownership. The ASF licenses this file 
    6     * to you under the Apache License, Version 2.0 (the 
    7     * "License"); you may not use this file except in compliance 
    8     * with the License. You may obtain a copy of the License at 
    9     * 
   10     * http://www.apache.org/licenses/LICENSE-2.0 
   11     * 
   12     * Unless required by applicable law or agreed to in writing, 
   13     * software distributed under the License is distributed on an 
   14     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 
   15     * KIND, either express or implied. See the License for the 
   16     * specific language governing permissions and limitations 
   17     * under the License. 
   18     */
   19   package org.apache.cxf.dosgi.samples.greeter.client;
   20   
   21   import java.util.Map;
   22   
   23   import org.apache.cxf.dosgi.samples.greeter.GreeterData;
   24   import org.apache.cxf.dosgi.samples.greeter.GreeterException;
   25   import org.apache.cxf.dosgi.samples.greeter.GreeterService;
   26   import org.apache.cxf.dosgi.samples.greeter.GreetingPhrase;
   27   import org.osgi.framework.BundleActivator;
   28   import org.osgi.framework.BundleContext;
   29   import org.osgi.framework.ServiceReference;
   30   import org.osgi.util.tracker.ServiceTracker;
   31   
   32   public class Activator implements BundleActivator {    
   33       private ServiceTracker tracker;
   34   
   35       public void start(final BundleContext bc) {
   36           tracker = new ServiceTracker(bc, GreeterService.class.getName(), null) {
   37               @Override
   38               public Object addingService(ServiceReference reference) {
   39                   Object result = super.addingService(reference);
   40   
   41                   useService(bc, reference);
   42                   
   43                   return result;
   44               }
   45           };
   46           tracker.open();
   47           
   48       }
   49   
   50       protected void useService(final BundleContext bc, ServiceReference reference) {
   51           Object svc = bc.getService(reference);
   52           if (!(svc instanceof GreeterService)) {
   53               return;
   54           }
   55           final GreeterService greeter = (GreeterService) svc;
   56   
   57           Thread t = new Thread(new Runnable() {
   58               public void run() {
   59                   greeterUI(bc, greeter);
   60               }
   61           });
   62           t.start();
   63       }
   64   
   65       private void greeterUI(final BundleContext bc, final GreeterService greeter) {
   66           while (true) {
   67               System.out.println("*** Opening greeter client dialog ***");
   68               Object gd = getGreeterData();
   69               if (gd instanceof String) {
   70                   System.out.println("*** Invoking greeter ***");
   71                   Map<GreetingPhrase, String> result = greeter.greetMe((String) gd);
   72   
   73                   System.out.println("greetMe(\"" + gd + "\") returns:");
   74                   for (Map.Entry<GreetingPhrase, String> greeting : result.entrySet()) {
   75                       System.out.println("  " + greeting.getKey().getPhrase() 
   76                               + " " + greeting.getValue());
   77                   }
   78               } else if (gd instanceof GreeterData) {
   79                   System.out.println("*** Invoking greeter ***");
   80                   try {
   81                       GreetingPhrase [] result = greeter.greetMe((GreeterData) gd);
   82                       System.out.println("greetMe(\"" + gd + "\") returns:");
   83                       for (GreetingPhrase phrase : result) {
   84                           System.out.println("  " + phrase.getPhrase());
   85                       }
   86                   } catch (GreeterException ex) {
   87                       System.out.println("GreeterException : " + ex.toString());
   88                   }                
   89               }
   90           }
   91       }
   92   
   93       private static Object getGreeterData() {
   94           GreeterDialog gd = new GreeterDialog();
   95           gd.setVisible(true);
   96           return gd.getSelection();
   97       }
   98   
   99       public void stop(BundleContext bc) throws Exception {
  100           tracker.close();
  101       }
  102   }

Home » cxf-dosgi-ri-source-distribution-1.1 » org.apache.cxf.dosgi.samples.greeter.client » [javadoc | source]