Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[udig-devel] IService implementation Q&A

I have had a couple chats to day about the change to IService; most implementations seem fine - as everyone made the same "mistake" and implemented a getInfo method...

I have updated the IService javadocs to include a code example about extending...

You may want to implement your own IService in order to provide a handle for a new kind of /API/.

  1. New method:

     |
       public API getAPI( ProgressMonitor monitor){
           if (monitor == null) monitor = new NullProgressMonitor();
monitor.beingTask("Connect to API",2); try { String server = getConnectionParams().get("server");
               monitor.worked(1);
               return new API( s );
           }
           finally {
               monitor.done();
           }
       }
       |

     (note the use of NullProgressMonitor)
  2. Optional: Customize resolve method to advertise your new API
     "dynamically"

     |
      public <T> boolean canResolve( Class<T> adaptee ) {
          return adaptee != null
                  && (adaptee.isAssignableFrom(API.class) || super.canResolve(adaptee));
      }
      public <T> T resolve( Class<T> adaptee, IProgressMonitor monitor ) throws IOException {
if (monitor == null) monitor = new NullProgressMonitor(); if (adaptee == null)
              throw new NullPointerException("No adaptor specified" );
if (adaptee.isAssignableFrom(API.class)) {
              return adaptee.cast(getAPI(monitor));
          }
          return super.resolve(adaptee, monitor);
      }
      |

     (note the call to super)
  3. Optional: cache your API as the "connection"

     |
       API api = null;
       Throwable msg = null;
       public synchronized API getAPI( ProgressMonitor monitor){
           if( api != null ) return api;
if (monitor == null) monitor = new NullProgressMonitor(); monitor.beingTask("Connect to API",2); try { String server = getConnectionParams().get("server");
               monitor.worked(1);
               api = new API( s );
               monitor.worked(1);
               return api;
           }
           finally {
               monitor.done();
           }
       }
       public Status getStatus() {
           return msg != null? Status.BROKEN : api == null? Status.NOTCONNECTED : Status.CONNECTED;
       }
       public Throwable getMessage(){
           return msg;
       }
       public synchronized void dispose( ProgressMonitor monitor ){
           if( api != null ){
                api.dispose();
                api = null;
           }
           if( msg != null ) msg = null;
       }
      |




Back to the top