protected TextService getServiceInstance ( Map < String , String > headers ) throws ServiceException { try { String requestPath = headers . get ( Listener . METAINFO_REQUEST_PATH ) ; String [ ] pathSegments = requestPath != null ? requestPath . split ( "/" ) : null ; if ( pathSegments == null ) throw new ServiceException ( ServiceException . INTERNAL_ERROR , "Unable to find a service or handler for request path: " + requestPath ) ; String contentType = headers . get ( Listener . METAINFO_CONTENT_TYPE ) ; String serviceClassName = MDW_REST_SERVICE_PROVIDER_PACKAGE + "." + pathSegments [ 0 ] ; try { // normal classloader -- built-in service Class < ? extends TextService > serviceClass = Class . forName ( serviceClassName ) . asSubclass ( TextService . class ) ; return serviceClass . newInstance ( ) ; } catch ( ClassNotFoundException ex ) { // try dynamic based on annotations eg: api/Users/dxoakes Class < ? extends RegisteredService > serviceType = Listener . CONTENT_TYPE_JSON . equals ( contentType ) ? JsonService . class : XmlService . class ; MdwServiceRegistry registry = MdwServiceRegistry . getInstance ( ) ; String pkgName = null ; for ( int i = 0 ; i < pathSegments . length ; i ++ ) { String pathSegment = pathSegments [ i ] ; if ( i == 0 ) pkgName = pathSegment ; else pkgName += "." + pathSegment ; Package pkg = PackageCache . getPackage ( pkgName ) ; if ( pkg != null ) { // try without any subpath first (@Path="/") TextService service = ( TextService ) registry . getDynamicServiceForPath ( pkg , serviceType , "/" ) ; if ( service == null && i < pathSegments . length - 1 ) { service = ( TextService ) registry . getDynamicServiceForPath ( pkg , serviceType , "/" + pathSegments [ i + 1 ] ) ; } if ( service != null ) return service ; } } // lastly, try process invoker mapping AssetRequest processRequest = ProcessRequests . getRequest ( headers . get ( Listener . METAINFO_HTTP_METHOD ) , requestPath ) ; if ( processRequest != null ) { return new ProcessInvoker ( processRequest ) ; } return null ; } } catch ( Exception ex ) { throw new ServiceException ( ServiceException . INTERNAL_ERROR , ex . getMessage ( ) , ex ) ; } }
Returns the service instance consulting the service registry if necessary .