protected PolicySubject lookup()

in modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/PolicyAttachmentBuilderImpl.java [322:411]


    protected PolicySubject lookup(Composite composite, String structuralURI) {
        if (structuralURI == null) {
            return null;
        } else if ( structuralURI.equals("")) {
        	return composite;
        }
        int index = structuralURI.indexOf('#');
        String componentURI = structuralURI;
        String service = null;
        String reference = null;
        String binding = null;
        boolean isInterface = false;
        boolean impl = false;
    	boolean isCallback = false;
    	
        if (index != -1) {
            componentURI = structuralURI.substring(0, index);
            String fragment = structuralURI.substring(index + 1);
            int begin = fragment.indexOf('(');
            int end = fragment.indexOf(')');
            if (begin != -1 && end != -1) {
                String path = fragment.substring(begin + 1, end).trim();
                String prefix = fragment.substring(0, begin).trim();
                if (prefix.equals("implementation")) {
                    impl = true;
                } else {                
                    int pos = path.indexOf('/');
                    if (pos != -1) {
                        binding = path.substring(pos + 1);
                        if ( binding.startsWith("callback/")) {
                        	binding = path.substring(pos + 10);                        	
                        	isCallback = true;
                        }
                        path = path.substring(0, pos);
                        if ("service-binding".equals(prefix)) {
                            service = path;
                        } else if ("reference-binding".equals(prefix)) {
                            reference = path;
                        }
                    }
                    if ("service".equals(prefix)) {
                        service = path;
                    } else if ("reference".equals(prefix)) {
                        reference = path;
                    } else if ( prefix.indexOf("#interface") != -1 ) {
                    	service = prefix.substring(0, prefix.indexOf("#interface"));
                    	isInterface = true;
                    }
                }
            }
        }
        for (Component component : composite.getComponents()) {
            if (component.getURI().equals(componentURI)) {
                if (service != null) {
                    ComponentService componentService = component.getService(service);
                    if ( isInterface ) {
                    	return componentService.getInterfaceContract().getInterface();
                    } else if (binding != null) {
                        Binding b = getBinding(componentService, binding, isCallback);
                        if (b instanceof PolicySubject) {
                            return (PolicySubject)b;
                        }
                    } else {
                        return componentService;
                    }
                } else if (reference != null) {
                    ComponentReference componentReference = component.getReference(reference);
                    if (binding != null) {
                        Binding b = getBinding(componentReference, binding, isCallback);
                        if (b instanceof PolicySubject) {
                            return (PolicySubject)b;
                        }
                    } else {
                        return componentReference;
                    }
                } else if (impl) {
                    return component.getImplementation();
                }
                return component;
            } else if (structuralURI.startsWith(component.getURI() + "/")) {
                Implementation implementation = component.getImplementation();
                if (implementation instanceof Composite) {
                    return lookup((Composite)implementation, structuralURI);
                } else {
                    return null;
                }
            }
        }
        return null;
    }