From 8f34ff7c71819472d7b2f023b8ecb04da97d2655 Mon Sep 17 00:00:00 2001 From: adrianocrestani Date: Sat, 6 Jun 2009 09:18:42 +0000 Subject: -applying patch tuscany_2552_phillipe_ramalho_06_04_2009.patch from TUSCANY-2552 -adding domain-search to modules/pom.xml git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@782215 13f79535-47bb-0310-9956-ffa450edef68 --- .../tuscany/sca/domain/search/DocumentMap.java | 25 +++ .../sca/domain/search/DocumentProcessor.java | 13 ++ .../sca/domain/search/DocumentProcessorsMap.java | 125 +++++++++++++ .../tuscany/sca/domain/search/DomainSearch.java | 37 ++++ .../SearchContributionListenerExtensionPoint.java | 33 ++++ .../search/impl/ArtifactDocumentProcessor.java | 68 +++++++ .../search/impl/BindingDocumentProcessor.java | 59 ++++++ .../search/impl/ComponentDocumentProcessor.java | 179 +++++++++++++++++++ .../impl/ComponentTypeDocumentProcessor.java | 173 ++++++++++++++++++ .../search/impl/CompositeDocumentProcessor.java | 106 +++++++++++ .../search/impl/ContributionDocumentProcessor.java | 120 +++++++++++++ ...ltSearchContributionListenerExtensionPoint.java | 35 ++++ .../search/impl/DirectoryDocumentProcessor.java | 50 ++++++ .../tuscany/sca/domain/search/impl/Document.java | 61 +++++++ .../sca/domain/search/impl/DomainPathAnalyzer.java | 197 +++++++++++++++++++++ .../domain/search/impl/DomainSearchAnalyzer.java | 14 ++ .../impl/DomainSearchDocumentProcessorsMap.java | 27 +++ .../sca/domain/search/impl/DomainSearchImpl.java | 140 +++++++++++++++ .../search/impl/DomainSearchModuleActivator.java | 20 +++ .../domain/search/impl/FileDocumentProcessor.java | 50 ++++++ .../sca/domain/search/impl/NamingAnalyzer.java | 27 +++ .../sca/domain/search/impl/NamingTokenizer.java | 127 +++++++++++++ .../search/impl/PropertyDocumentProcessor.java | 63 +++++++ .../sca/domain/search/impl/SearchFields.java | 55 ++++++ .../search/impl/DomainPathAnalyzerTestCase.java | 41 +++++ .../search/impl/NamingTokenizerTestCase.java | 140 +++++++++++++++ 26 files changed, 1985 insertions(+) create mode 100644 branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/DocumentMap.java create mode 100644 branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/DocumentProcessor.java create mode 100644 branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/DocumentProcessorsMap.java create mode 100644 branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/DomainSearch.java create mode 100644 branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/SearchContributionListenerExtensionPoint.java create mode 100644 branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/ArtifactDocumentProcessor.java create mode 100644 branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/BindingDocumentProcessor.java create mode 100644 branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/ComponentDocumentProcessor.java create mode 100644 branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/ComponentTypeDocumentProcessor.java create mode 100644 branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/CompositeDocumentProcessor.java create mode 100644 branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/ContributionDocumentProcessor.java create mode 100644 branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/DefaultSearchContributionListenerExtensionPoint.java create mode 100644 branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/DirectoryDocumentProcessor.java create mode 100644 branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/Document.java create mode 100644 branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/DomainPathAnalyzer.java create mode 100644 branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/DomainSearchAnalyzer.java create mode 100644 branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/DomainSearchDocumentProcessorsMap.java create mode 100644 branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/DomainSearchImpl.java create mode 100644 branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/DomainSearchModuleActivator.java create mode 100644 branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/FileDocumentProcessor.java create mode 100644 branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/NamingAnalyzer.java create mode 100644 branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/NamingTokenizer.java create mode 100644 branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/PropertyDocumentProcessor.java create mode 100644 branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/SearchFields.java create mode 100644 branches/sca-java-1.x/modules/domain-search/src/test/java/org/apache/tuscany/sca/domain/search/impl/DomainPathAnalyzerTestCase.java create mode 100644 branches/sca-java-1.x/modules/domain-search/src/test/java/org/apache/tuscany/sca/domain/search/impl/NamingTokenizerTestCase.java (limited to 'branches/sca-java-1.x/modules/domain-search/src') diff --git a/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/DocumentMap.java b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/DocumentMap.java new file mode 100644 index 0000000000..8df1f70269 --- /dev/null +++ b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/DocumentMap.java @@ -0,0 +1,25 @@ +package org.apache.tuscany.sca.domain.search; + +import java.util.HashMap; + +import org.apache.tuscany.sca.domain.search.impl.Document; + +public class DocumentMap extends HashMap { + + private static final long serialVersionUID = -2402910290314939928L; + + @Override + public Document get(Object key) { + Document doc = super.get(key); + + if (doc == null) { + doc = new Document(); + put(key, doc); + + } + + return doc; + + } + +} diff --git a/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/DocumentProcessor.java b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/DocumentProcessor.java new file mode 100644 index 0000000000..6a3de9e8c9 --- /dev/null +++ b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/DocumentProcessor.java @@ -0,0 +1,13 @@ +package org.apache.tuscany.sca.domain.search; + +import org.apache.tuscany.sca.domain.search.impl.Document; + +public interface DocumentProcessor { + + final public static Document FAKE_DOCUMENT = new Document(); + + Document process(DocumentProcessorsMap processors, DocumentMap documents, Object object, Document document, String parent); + + Object getDocumentKey(Object object); + +} diff --git a/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/DocumentProcessorsMap.java b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/DocumentProcessorsMap.java new file mode 100644 index 0000000000..19c9e51d8d --- /dev/null +++ b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/DocumentProcessorsMap.java @@ -0,0 +1,125 @@ +package org.apache.tuscany.sca.domain.search; + +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; + +import org.apache.tuscany.sca.domain.search.impl.Document; + +public class DocumentProcessorsMap extends + HashMap, List> implements DocumentProcessor { + + private static final long serialVersionUID = 3967390896890947159L; + + private Object documentKey; + + public void addDocumentProcessor(Class clazz, DocumentProcessor processor) { + List processors = get(clazz); + + if (processors == null) { + processors = new LinkedList(); + put(clazz, processors); + + } + + processors.add(processor); + + } + + private void appendProcessors(LinkedList processorsList, + List processors, Object object) { + + if (processors != null) { + + for (DocumentProcessor processor : processors) { + + if (this.documentKey == null) { + this.documentKey = processor.getDocumentKey(object); + + if (processorsList == null) { + return; + } + + } + + if (processorsList != null) { + processorsList.add(processor); + } + + } + + } + + } + + private void findAllDocumentProcessors( + LinkedList processorsList, Object object) { + Class clazz = object.getClass(); + appendProcessors(processorsList, get(clazz), object); + + while (clazz != null) { + Class[] interfaces = clazz.getInterfaces(); + + for (Class interfac : interfaces) { + Class[] interfaces2 = interfac.getInterfaces(); + appendProcessors(processorsList, get(interfac), object); + + for (Class interface2 : interfaces2) { + appendProcessors(processorsList, get(interface2), object); + } + + } + + clazz = clazz.getSuperclass(); + appendProcessors(processorsList, get(clazz), object); + + } + + } + + public Document process(DocumentProcessorsMap processors, + DocumentMap documents, Object object, Document document, String parent) { + + LinkedList processorsList; + + try { + + this.documentKey = document; + processorsList = new LinkedList(); + findAllDocumentProcessors(processorsList, object); + + if (document == null && this.documentKey != null) { + document = documents.get(this.documentKey); + + if (document == null) { + document = FAKE_DOCUMENT; + } + + } + + } finally { + this.documentKey = null; + } + + for (DocumentProcessor processor : processorsList) { + processor.process(processors, documents, object, document, parent); + } + + return document; + + } + + public Object getDocumentKey(Object object) { + + try { + findAllDocumentProcessors(null, object); + + return this.documentKey; + + } finally { + this.documentKey = null; + } + + } + +} diff --git a/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/DomainSearch.java b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/DomainSearch.java new file mode 100644 index 0000000000..3d26b08d8a --- /dev/null +++ b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/DomainSearch.java @@ -0,0 +1,37 @@ +package org.apache.tuscany.sca.domain.search; + +import org.apache.tuscany.sca.contribution.Contribution; +import org.apache.tuscany.sca.contribution.service.ContributionRepository; +import org.osoa.sca.annotations.Remotable; + +@Remotable +public interface DomainSearch { + + + + /** + * Notifies the listener that a contribution has been added. + * + * @param repository The contribution repository + * @param contribution The new contribution + */ + void contributionAdded(ContributionRepository repository, Contribution contribution); + + /** + * Notifies the listener that a contribution has been removed. + * + * @param repository The contribution repository + * @param contribution The removed contribution. + */ + void contributionRemoved(ContributionRepository repository, Contribution contribution); + + /** + * Notifies the listener that a contribution has been updated. + * + * @param repository The contribution repository + * @param oldContribution The old contribution + * @param contribution The new contribution + */ + void contributionUpdated(ContributionRepository repository, Contribution oldContribution, Contribution contribution); + +} diff --git a/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/SearchContributionListenerExtensionPoint.java b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/SearchContributionListenerExtensionPoint.java new file mode 100644 index 0000000000..0138ac6a39 --- /dev/null +++ b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/SearchContributionListenerExtensionPoint.java @@ -0,0 +1,33 @@ +package org.apache.tuscany.sca.domain.search; + +import org.apache.tuscany.sca.contribution.Contribution; +import org.apache.tuscany.sca.contribution.service.ContributionRepository; + +public interface SearchContributionListenerExtensionPoint { + + /** + * Notifies the listener that a contribution has been added. + * + * @param repository The contribution repository + * @param contribution The new contribution + */ + void contributionAdded(ContributionRepository repository, Contribution contribution); + + /** + * Notifies the listener that a contribution has been removed. + * + * @param repository The contribution repository + * @param contribution The removed contribution. + */ + void contributionRemoved(ContributionRepository repository, Contribution contribution); + + /** + * Notifies the listener that a contribution has been updated. + * + * @param repository The contribution repository + * @param oldContribution The old contribution + * @param contribution The new contribution + */ + void contributionUpdated(ContributionRepository repository, Contribution oldContribution, Contribution contribution); + +} diff --git a/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/ArtifactDocumentProcessor.java b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/ArtifactDocumentProcessor.java new file mode 100644 index 0000000000..815f0a20c7 --- /dev/null +++ b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/ArtifactDocumentProcessor.java @@ -0,0 +1,68 @@ +package org.apache.tuscany.sca.domain.search.impl; + +import org.apache.lucene.document.Field; +import org.apache.tuscany.sca.contribution.Artifact; +import org.apache.tuscany.sca.domain.search.DocumentMap; +import org.apache.tuscany.sca.domain.search.DocumentProcessor; +import org.apache.tuscany.sca.domain.search.DocumentProcessorsMap; + +public class ArtifactDocumentProcessor implements DocumentProcessor { + + public Document process(DocumentProcessorsMap processors, + DocumentMap documents, Object object, Document document, String parent) { + + if (object instanceof Artifact) { + Artifact artifact = (Artifact) object; + String uri = artifact.getURI(); + + if (uri != null && uri.length() == 0) { + uri = null; + } + + if (uri != null) { + + parent += DomainPathAnalyzer.PATH_SEPARATOR + SearchFields.ARTIFACT_FIELD + DomainPathAnalyzer.TYPE_SEPARATOR + uri; + + if (document == null) { + document = documents.get(uri); + } + + document.add(new Field(SearchFields.ARTIFACT_FIELD, uri, + Field.Store.YES, Field.Index.ANALYZED)); + + } + + if (document != null) { + + document.add(new Field(SearchFields.LOCATION_FIELD, artifact.getLocation(), + Field.Store.YES, Field.Index.ANALYZED)); + + } + + return document == null ? FAKE_DOCUMENT : document; + + } + + throw new IllegalArgumentException(); + + } + + public Object getDocumentKey(Object obj) { + + if (obj instanceof Artifact) { + Artifact artifact = (Artifact) obj; + String uri = artifact.getURI(); + + if (uri != null && uri.length() == 0) { + return null; + } + + return uri; + + } + + throw new IllegalArgumentException(); + + } + +} diff --git a/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/BindingDocumentProcessor.java b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/BindingDocumentProcessor.java new file mode 100644 index 0000000000..5d01bec389 --- /dev/null +++ b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/BindingDocumentProcessor.java @@ -0,0 +1,59 @@ +package org.apache.tuscany.sca.domain.search.impl; + +import org.apache.lucene.document.Field; +import org.apache.tuscany.sca.assembly.Binding; +import org.apache.tuscany.sca.domain.search.DocumentMap; +import org.apache.tuscany.sca.domain.search.DocumentProcessor; +import org.apache.tuscany.sca.domain.search.DocumentProcessorsMap; + +public class BindingDocumentProcessor implements DocumentProcessor { + + public Document process(DocumentProcessorsMap processors, + DocumentMap documents, Object object, Document document, String parent) { + + if (object instanceof Binding) { + Binding binding = (Binding) object; + String uri = binding.getURI(); + + if (uri != null && uri.length() == 0) { + uri = null; + } + + if (uri != null) { + + if (document == null) { + document = documents.get(uri); + } + + document.add(new Field(SearchFields.BINDING_FIELD, uri, + Field.Store.YES, Field.Index.ANALYZED)); + + } + + return document == null ? FAKE_DOCUMENT : document; + + } + + throw new IllegalArgumentException(); + + } + + public Object getDocumentKey(Object obj) { + + if (obj instanceof Binding) { + Binding binding = (Binding) obj; + String uri = binding.getURI(); + + if (uri != null && uri.length() == 0) { + return null; + } + + return uri; + + } + + throw new IllegalArgumentException(); + + } + +} diff --git a/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/ComponentDocumentProcessor.java b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/ComponentDocumentProcessor.java new file mode 100644 index 0000000000..284791118a --- /dev/null +++ b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/ComponentDocumentProcessor.java @@ -0,0 +1,179 @@ +package org.apache.tuscany.sca.domain.search.impl; + +import org.apache.lucene.document.Field; +import org.apache.tuscany.sca.assembly.Component; +import org.apache.tuscany.sca.assembly.ComponentProperty; +import org.apache.tuscany.sca.assembly.ComponentReference; +import org.apache.tuscany.sca.assembly.ComponentService; +import org.apache.tuscany.sca.domain.search.DocumentMap; +import org.apache.tuscany.sca.domain.search.DocumentProcessor; +import org.apache.tuscany.sca.domain.search.DocumentProcessorsMap; +import org.apache.tuscany.sca.interfacedef.InterfaceContract; +import org.apache.tuscany.sca.interfacedef.Operation; + +public class ComponentDocumentProcessor implements DocumentProcessor { + + public Document process(DocumentProcessorsMap processors, + DocumentMap documents, Object object, Document doc, String parent) { + + if (object instanceof Component) { + Component component = (Component) object; + String uri = component.getURI(); + String name = component.getName(); + + uri = (uri == null ? "" : uri) + (name == null ? "" : name); + + if (uri.length() == 0) { + uri = null; + } + + if (uri != null) { + + if (doc == null) { + doc = documents.get(uri); + } + + parent += DomainPathAnalyzer.PATH_SEPARATOR + SearchFields.COMPONENT_FIELD + DomainPathAnalyzer.TYPE_SEPARATOR + uri; + + doc.add(new Field(SearchFields.COMPONENT_FIELD, uri, + Field.Store.YES, Field.Index.ANALYZED)); + + for (ComponentService service : component.getServices()) { + Document serviceDoc = documents.get(uri + ':' + + service.getName()); + + serviceDoc.add(new Field(SearchFields.SERVICE_NAME_FIELD, + service.getName(), Field.Store.YES, + Field.Index.ANALYZED)); + + InterfaceContract interfaceContract = service + .getInterfaceContract(); + + if (interfaceContract != null) { + + for (Operation operation : interfaceContract + .getInterface().getOperations()) { + + serviceDoc.add(new Field( + SearchFields.SERVICE_INTERFACE_FIELD, + operation.getName(), Field.Store.YES, + Field.Index.ANALYZED)); + + } + + for (Operation operation : interfaceContract + .getCallbackInterface().getOperations()) { + + serviceDoc + .add(new Field( + SearchFields.SERVICE_INTERFACE_CALLBACK_FIELD, + operation.getName(), + Field.Store.YES, + Field.Index.ANALYZED)); + + } + + } + + serviceDoc.add(new Field(SearchFields.PARENT_FIELD, parent, + Field.Store.YES, Field.Index.ANALYZED)); + + } + + for (ComponentReference reference : component.getReferences()) { + Document serviceDoc = documents.get(uri + ':' + + reference.getName()); + + serviceDoc.add(new Field(SearchFields.REFERENCE_NAME_FIELD, + reference.getName(), Field.Store.YES, + Field.Index.ANALYZED)); + + InterfaceContract interfaceContract = reference + .getInterfaceContract(); + + if (interfaceContract != null) { + + for (Operation operation : interfaceContract + .getInterface().getOperations()) { + + serviceDoc.add(new Field( + SearchFields.REFERENCE_INTERFACE_FIELD, + operation.getName(), Field.Store.YES, + Field.Index.ANALYZED)); + + } + + for (Operation operation : interfaceContract + .getCallbackInterface().getOperations()) { + + serviceDoc + .add(new Field( + SearchFields.REFERENCE_INTERFACE_CALLBACK_FIELD, + operation.getName(), + Field.Store.YES, + Field.Index.ANALYZED)); + + } + + } + + serviceDoc.add(new Field(SearchFields.PARENT_FIELD, parent, + Field.Store.YES, Field.Index.ANALYZED)); + + } + + } + + Document implementationDoc = processors.process(processors, + documents, component.getImplementation(), null, parent); + + if (uri != null && implementationDoc != null) { + + implementationDoc.add(new Field(SearchFields.IMPLEMENTS_FIELD, + uri, Field.Store.YES, Field.Index.ANALYZED)); + + } + + for (ComponentProperty componentProperty : component + .getProperties()) { + Document propertyDoc = processors.process(processors, + documents, componentProperty.getProperty(), null, parent); + + if (uri != null) { + propertyDoc.add(new Field(SearchFields.PARENT_FIELD, parent, + Field.Store.YES, Field.Index.ANALYZED)); + + } + + } + + return doc == null ? FAKE_DOCUMENT : doc; + + } + + throw new IllegalArgumentException(); + + } + + public Object getDocumentKey(Object obj) { + + if (obj instanceof Component) { + Component component = (Component) obj; + String uri = component.getURI(); + String name = component.getName(); + + uri = (uri == null ? "" : uri) + (name == null ? "" : name); + + if (uri.length() == 0) { + return null; + } + + return uri; + + } + + throw new IllegalArgumentException(); + + } + +} diff --git a/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/ComponentTypeDocumentProcessor.java b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/ComponentTypeDocumentProcessor.java new file mode 100644 index 0000000000..b110ce769d --- /dev/null +++ b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/ComponentTypeDocumentProcessor.java @@ -0,0 +1,173 @@ +package org.apache.tuscany.sca.domain.search.impl; + +import org.apache.lucene.document.Field; +import org.apache.tuscany.sca.assembly.ComponentType; +import org.apache.tuscany.sca.assembly.Property; +import org.apache.tuscany.sca.assembly.Reference; +import org.apache.tuscany.sca.assembly.Service; +import org.apache.tuscany.sca.domain.search.DocumentMap; +import org.apache.tuscany.sca.domain.search.DocumentProcessor; +import org.apache.tuscany.sca.domain.search.DocumentProcessorsMap; +import org.apache.tuscany.sca.interfacedef.InterfaceContract; +import org.apache.tuscany.sca.interfacedef.Operation; + +public class ComponentTypeDocumentProcessor implements DocumentProcessor { + + public Document process(DocumentProcessorsMap processors, + DocumentMap documents, Object object, Document doc, String parent) { + + if (object instanceof ComponentType) { + ComponentType componentType = (ComponentType) object; + String uri = componentType.getURI(); + + if (uri != null && uri.length() == 0) { + uri = null; + } + + if (doc == null) { + + if (uri != null) { + doc = documents.get(uri); + + } else { + doc = FAKE_DOCUMENT; + } + + } + + if (uri != null) { + + parent += DomainPathAnalyzer.PATH_SEPARATOR + SearchFields.COMPONENT_TYPE_FIELD + DomainPathAnalyzer.TYPE_SEPARATOR + uri; + + doc.add(new Field(SearchFields.COMPONENT_TYPE_FIELD, uri, + Field.Store.YES, Field.Index.ANALYZED)); + + for (Service service : componentType.getServices()) { + Document serviceDoc = documents.get(uri + ':' + + service.getName()); + + serviceDoc.add(new Field(SearchFields.SERVICE_NAME_FIELD, + service.getName(), Field.Store.YES, + Field.Index.ANALYZED)); + + InterfaceContract interfaceContract = service + .getInterfaceContract(); + + if (interfaceContract != null) { + + for (Operation operation : interfaceContract + .getInterface().getOperations()) { + + serviceDoc.add(new Field( + SearchFields.SERVICE_INTERFACE_FIELD, + operation.getName(), Field.Store.YES, + Field.Index.ANALYZED)); + + } + + for (Operation operation : interfaceContract + .getCallbackInterface().getOperations()) { + + serviceDoc + .add(new Field( + SearchFields.SERVICE_INTERFACE_CALLBACK_FIELD, + operation.getName(), + Field.Store.YES, + Field.Index.ANALYZED)); + + } + + } + + serviceDoc.add(new Field(SearchFields.PARENT_FIELD, parent, + Field.Store.YES, Field.Index.ANALYZED)); + + } + + for (Reference reference : componentType.getReferences()) { + Document referenceDoc = documents.get(componentType + .getURI() + + ':' + reference.getName()); + + referenceDoc.add(new Field( + SearchFields.REFERENCE_NAME_FIELD, reference + .getName(), Field.Store.YES, + Field.Index.ANALYZED)); + + InterfaceContract interfaceContract = reference + .getInterfaceContract(); + + if (interfaceContract != null) { + + for (Operation operation : interfaceContract + .getInterface().getOperations()) { + + referenceDoc.add(new Field( + SearchFields.REFERENCE_INTERFACE_FIELD, + operation.getName(), Field.Store.YES, + Field.Index.ANALYZED)); + + } + + for (Operation operation : interfaceContract + .getCallbackInterface().getOperations()) { + + referenceDoc + .add(new Field( + SearchFields.REFERENCE_INTERFACE_CALLBACK_FIELD, + operation.getName(), + Field.Store.YES, + Field.Index.ANALYZED)); + + } + + } + + referenceDoc.add(new Field(SearchFields.PARENT_FIELD, parent, + Field.Store.YES, Field.Index.ANALYZED)); + + } + + } + + for (Property property : componentType.getProperties()) { + Document propertyDoc = processors.process(processors, + documents, property, null, parent); + + if (uri != null) { + + propertyDoc.add(new Field(SearchFields.PARENT_FIELD, parent, + Field.Store.YES, Field.Index.ANALYZED)); + + } + + } + + return doc; + + } + + throw new IllegalArgumentException(); + + } + + public Object getDocumentKey(Object object) { + + if (object instanceof ComponentType) { + ComponentType componentType = (ComponentType) object; + String uri = componentType.getURI(); + + if (uri == null || uri.length() == 0) { + return null; + + } else { + return uri; + } + + } + + throw new IllegalArgumentException(); + + } + +} diff --git a/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/CompositeDocumentProcessor.java b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/CompositeDocumentProcessor.java new file mode 100644 index 0000000000..27e1e9b087 --- /dev/null +++ b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/CompositeDocumentProcessor.java @@ -0,0 +1,106 @@ +package org.apache.tuscany.sca.domain.search.impl; + +import javax.xml.namespace.QName; + +import org.apache.lucene.document.Field; +import org.apache.tuscany.sca.assembly.Component; +import org.apache.tuscany.sca.assembly.Composite; +import org.apache.tuscany.sca.domain.search.DocumentMap; +import org.apache.tuscany.sca.domain.search.DocumentProcessor; +import org.apache.tuscany.sca.domain.search.DocumentProcessorsMap; + +public class CompositeDocumentProcessor implements DocumentProcessor { + + public Document process(DocumentProcessorsMap processors, + DocumentMap documents, Object object, Document doc, String parent) { + + if (object instanceof Composite) { + Composite composite = (Composite) object; + String uri = composite.getURI(); + QName name = composite.getName(); + + uri = (uri == null ? "" : uri) + (name == null ? "" : name.toString()); + + if (uri.length() == 0) { + uri = null; + + } else if (doc == null) { + doc = documents.get(uri); + parent += DomainPathAnalyzer.PATH_SEPARATOR + SearchFields.COMPOSITE_FIELD + DomainPathAnalyzer.TYPE_SEPARATOR + uri; + + } + + for (Component component : composite.getComponents()) { + + + Document componentDoc = processors.process(processors, + documents, component, null, parent); + + if (uri != null) { + + componentDoc.add(new Field(SearchFields.PARENT_FIELD, parent, + Field.Store.YES, Field.Index.ANALYZED)); + + } + + } + + if (uri != null) { + + doc.add(new Field(SearchFields.COMPOSITE_FIELD, uri.toString(), + Field.Store.YES, Field.Index.ANALYZED)); + + } + + for (Composite include : composite.getIncludes()) { + Document compositeDoc = processors.process(processors, + documents, include, null, parent); + + if (uri != null) { + + compositeDoc.add(new Field(SearchFields.INCLUDEDBY_FIELD, + uri, Field.Store.YES, Field.Index.ANALYZED)); + + } + + } + + for (Component component : composite.getComponents()) { + Document componentDoc = processors.process(processors, + documents, component, null, parent); + + if (uri != null) { + + componentDoc.add(new Field(SearchFields.PARENT_FIELD, parent, + Field.Store.YES, Field.Index.ANALYZED)); + + } + + } + + return doc == null ? FAKE_DOCUMENT : doc; + + } + + throw new IllegalArgumentException(); + + } + + public Object getDocumentKey(Object object) { + + if (object instanceof Composite) { + Composite composite = (Composite) object; + String uri = composite.getURI(); + QName name = composite.getName(); + + uri = (uri == null ? "" : uri) + (name == null ? "" : name.toString()); + + return uri.length() == 0 ? null : uri; + + } + + throw new IllegalArgumentException(); + + } + +} diff --git a/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/ContributionDocumentProcessor.java b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/ContributionDocumentProcessor.java new file mode 100644 index 0000000000..86e0e98f95 --- /dev/null +++ b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/ContributionDocumentProcessor.java @@ -0,0 +1,120 @@ +package org.apache.tuscany.sca.domain.search.impl; + +import org.apache.lucene.document.Field; + +import org.apache.tuscany.sca.assembly.Composite; +import org.apache.tuscany.sca.contribution.Artifact; +import org.apache.tuscany.sca.contribution.Contribution; +import org.apache.tuscany.sca.domain.search.DocumentMap; +import org.apache.tuscany.sca.domain.search.DocumentProcessor; +import org.apache.tuscany.sca.domain.search.DocumentProcessorsMap; + +public class ContributionDocumentProcessor implements DocumentProcessor { + + public Document process(DocumentProcessorsMap processors, + DocumentMap documents, Object object, Document doc, String parent) { + + if (object instanceof Contribution) { + Contribution contribution = (Contribution) object; + String uri = contribution.getURI(); + + if (uri != null) { + + if (uri.length() == 0) { + uri = null; + + } else { + parent += DomainPathAnalyzer.PATH_SEPARATOR + SearchFields.CONTRIBUTION_FIELD + DomainPathAnalyzer.TYPE_SEPARATOR + uri; + } + + } + + if (uri != null && doc == null) { + doc = documents.get(uri); + + doc.add(new Field(SearchFields.CONTRIBUTION_FIELD, uri, + Field.Store.YES, Field.Index.ANALYZED)); + + } else { + doc = FAKE_DOCUMENT; + } + + for (Artifact artifact : contribution.getArtifacts()) { + Document artifactDoc = processors.process(processors, + documents, artifact, null, parent); + + if (uri != null) { + + artifactDoc.add(new Field(SearchFields.PARENT_FIELD, parent, + Field.Store.YES, Field.Index.ANALYZED)); + + } + + } + +// for (Import imprt : contribution.getImports()) { +// Document importDoc = processors.process(processors, documents, +// imprt, null); +// +// if (uri != null) { +// +// importDoc.add(new Field(SearchFields.IMPORTEDBY_FIELD, uri, +// Field.Store.YES, Field.Index.ANALYZED)); +// +// } +// +// } +// +// for (Export export : contribution.getExports()) { +// Document exportDoc = processors.process(processors, documents, +// export, null); +// +// if (uri != null) { +// +// exportDoc.add(new Field(SearchFields.EXPORTEDBY_FIELD, uri, +// Field.Store.YES, Field.Index.ANALYZED)); +// +// } +// +// } + + for (Composite composite : contribution.getDeployables()) { + Document compositeDoc = processors.process(processors, + documents, composite, null, parent); + + if (uri != null) { + + compositeDoc.add(new Field(SearchFields.PARENT_FIELD, parent, + Field.Store.YES, Field.Index.ANALYZED)); + + } + + } + + return doc; + + } + + throw new IllegalArgumentException(); + + } + + public Object getDocumentKey(Object object) { + + if (object instanceof Contribution) { + Contribution contribution = (Contribution) object; + String uri = contribution.getURI(); + + if (uri != null && uri.length() == 0) { + return null; + } + + return uri; + + } + + throw new IllegalArgumentException(); + + } + +} diff --git a/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/DefaultSearchContributionListenerExtensionPoint.java b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/DefaultSearchContributionListenerExtensionPoint.java new file mode 100644 index 0000000000..6f21983a2e --- /dev/null +++ b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/DefaultSearchContributionListenerExtensionPoint.java @@ -0,0 +1,35 @@ +package org.apache.tuscany.sca.domain.search.impl; + +import org.apache.tuscany.sca.contribution.Contribution; +import org.apache.tuscany.sca.contribution.service.ContributionRepository; +import org.apache.tuscany.sca.domain.search.SearchContributionListenerExtensionPoint; + +public class DefaultSearchContributionListenerExtensionPoint implements SearchContributionListenerExtensionPoint { + + public void contributionAdded(ContributionRepository repository, + Contribution contribution) { + System.out.println("contributionAdded:"); + System.out.println(repository); + System.out.println(contribution); + + } + + public void contributionRemoved(ContributionRepository repository, + Contribution contribution) { + System.out.println("contributionRemoved:"); + System.out.println(repository); + System.out.println(contribution); + + } + + public void contributionUpdated(ContributionRepository repository, + Contribution oldContribution, Contribution contribution) { + + System.out.println("contributionUpdated:"); + System.out.println(repository); + System.out.println(oldContribution); + System.out.println(contribution); + + } + +} diff --git a/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/DirectoryDocumentProcessor.java b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/DirectoryDocumentProcessor.java new file mode 100644 index 0000000000..95d008250b --- /dev/null +++ b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/DirectoryDocumentProcessor.java @@ -0,0 +1,50 @@ +package org.apache.tuscany.sca.domain.search.impl; + +import java.io.File; + +import org.apache.lucene.document.Field; +import org.apache.tuscany.sca.domain.search.DocumentMap; +import org.apache.tuscany.sca.domain.search.DocumentProcessor; +import org.apache.tuscany.sca.domain.search.DocumentProcessorsMap; + +public class DirectoryDocumentProcessor implements DocumentProcessor { + + public Document process(DocumentProcessorsMap processors, + DocumentMap documents, Object object, Document doc, String parent) { + + if (object instanceof File) { + File file = (File) object; + doc = documents.get(file.getAbsoluteFile()); + + if (file.isDirectory()) { + doc.add(new Field(SearchFields.DIRECTORY_FIELD, file.getAbsolutePath(), + Field.Store.YES, Field.Index.ANALYZED)); + + parent += DomainPathAnalyzer.PATH_SEPARATOR + SearchFields.DIRECTORY_FIELD + DomainPathAnalyzer.TYPE_SEPARATOR + file.getName(); + + File[] files = file.listFiles(); + + for (File childFile : files) { + Document fileDoc = processors.process(processors, documents, childFile, null, parent); + + fileDoc.add(new Field(SearchFields.PARENT_FIELD, parent, + Field.Store.YES, Field.Index.ANALYZED)); + + } + + } + + return doc; + + } + + throw new IllegalArgumentException(); + + } + + public Object getDocumentKey(Object object) { + // TODO Auto-generated method stub + return null; + } + +} diff --git a/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/Document.java b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/Document.java new file mode 100644 index 0000000000..8a1aac2cd5 --- /dev/null +++ b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/Document.java @@ -0,0 +1,61 @@ +package org.apache.tuscany.sca.domain.search.impl; + +import java.util.Collection; +import java.util.Collections; +import java.util.Hashtable; + +import org.apache.lucene.document.Fieldable; + +public class Document { + + private Hashtable> fieldablesTable = new Hashtable>(); + + public Document() { + // empty constructor + } + + public void add(Fieldable fieldable) { + Hashtable fieldables = this.fieldablesTable + .get(fieldable.name()); + + if (fieldables == null) { + fieldables = new Hashtable(); + this.fieldablesTable.put(fieldable.name(), fieldables); + + } + + fieldables.put(fieldable.stringValue(), fieldable); + + } + + public org.apache.lucene.document.Document createLuceneDocument() { + org.apache.lucene.document.Document doc = new org.apache.lucene.document.Document(); + + for (Hashtable fieldables : this.fieldablesTable.values()) { + + for (Fieldable fieldable : fieldables.values()) { + doc.add(fieldable); + } + + } + + return doc; + + } + + public Collection getFieldValues(String field) { + Hashtable fieldables = this.fieldablesTable.get(field); + + if (fieldables != null) { + return fieldables.keySet(); + } + + return Collections.EMPTY_LIST; + + } + + public boolean containsField(String field) { + return this.fieldablesTable.containsKey(field); + } + +} diff --git a/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/DomainPathAnalyzer.java b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/DomainPathAnalyzer.java new file mode 100644 index 0000000000..9255f49eef --- /dev/null +++ b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/DomainPathAnalyzer.java @@ -0,0 +1,197 @@ +package org.apache.tuscany.sca.domain.search.impl; + +import java.io.IOException; +import java.io.Reader; + +import org.apache.lucene.analysis.Analyzer; +import org.apache.lucene.analysis.Token; +import org.apache.lucene.analysis.TokenStream; +import org.apache.lucene.analysis.Tokenizer; + +public class DomainPathAnalyzer extends Analyzer { + + final public static char PATH_SEPARATOR = '\u0001'; + + final public static char TYPE_SEPARATOR = '\u0002'; + + static class DomainPathTokenizer extends Tokenizer { + + private int offset = 0, bufferIndex = 0, dataLen = 0; + private static final int MAX_WORD_LEN = 255; + private static final int IO_BUFFER_SIZE = 4096; + private final char[] ioBuffer = new char[IO_BUFFER_SIZE]; + private boolean typeCharFound = false; + + public DomainPathTokenizer(Reader reader) { + super(reader); + } + + @Override + public void reset() throws IOException { + super.reset(); + + typeCharFound = false; + + } + + @Override + public void reset(Reader input) throws IOException { + super.reset(input); + + typeCharFound = false; + + } + + @Override + public Token next(Token reusableToken) throws IOException { + assert reusableToken != null; + reusableToken.clear(); + int length = 0; + int start = bufferIndex; + char[] buffer = reusableToken.termBuffer(); + + boolean lowercaseCharFound = false; + boolean digitFound = false; + + while (true) { + + if (bufferIndex >= dataLen) { + offset += dataLen; + int incr; + + if (lowercaseCharFound || length == 0) { + incr = 0; + + } else { + incr = 2; + ioBuffer[0] = ioBuffer[bufferIndex - 1]; + ioBuffer[1] = ioBuffer[bufferIndex]; + + } + + dataLen = input + .read(ioBuffer, incr, ioBuffer.length - incr); + if (dataLen == -1) { + if (length > 0) + break; + else + return null; + } + bufferIndex = incr; + dataLen += incr; + + } + + final char c = ioBuffer[bufferIndex++]; + boolean breakChar = true; + boolean includeChar = false; + + if (c == PATH_SEPARATOR) { + + if (length == 0) { + includeChar = true; + + } else { + bufferIndex--; + } + + typeCharFound = false; + + } else if (c == TYPE_SEPARATOR && !typeCharFound) { + length = 0; + breakChar = false; + lowercaseCharFound = false; + digitFound = false; + + } else { + + if (Character.isDigit(c)) { + + if (digitFound || length == 0) { + breakChar = false; + digitFound = true; + + } else { + bufferIndex--; + } + + // TODO: normalize accent, it does not index accents for + // now + } else if (c >= 65 && c <= 90 || c >= 97 && c <= 122) { + + if (digitFound) { + bufferIndex--; + + } else if (Character.isLowerCase(c)) { + + if (!(lowercaseCharFound || length <= 1)) { + length--; + bufferIndex -= 2; + + } else { + lowercaseCharFound = true; + breakChar = false; + + } + + } else if (!lowercaseCharFound) { // && uppercase + breakChar = false; + + } else { + bufferIndex--; + } + + } + + } + + if (!breakChar || includeChar) { + + if (length == 0) // start of token + start = offset + bufferIndex - 1; + else if (length == buffer.length) + buffer = reusableToken.resizeTermBuffer(1 + length); + + if (c == TYPE_SEPARATOR && !typeCharFound) { + typeCharFound = true; + + } else { + buffer[length++] = Character.toLowerCase(c); // buffer it, normalized + } + + if (length == MAX_WORD_LEN || (breakChar && length > 0)) // buffer overflow! + break; + + } else if (length > 0) {// at non-Letter w/ chars + + break; // return 'em + + } + + } + + reusableToken.setTermLength(length); + reusableToken.setStartOffset(start); + reusableToken.setEndOffset(start + length); + + return reusableToken; + + } + } + + public TokenStream tokenStream(String fieldName, Reader reader) { + return new DomainPathTokenizer(reader); + } + + public TokenStream reusableTokenStream(String fieldName, Reader reader) + throws IOException { + Tokenizer tokenizer = (Tokenizer) getPreviousTokenStream(); + if (tokenizer == null) { + tokenizer = new DomainPathTokenizer(reader); + setPreviousTokenStream(tokenizer); + } else + tokenizer.reset(reader); + return tokenizer; + } + +} diff --git a/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/DomainSearchAnalyzer.java b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/DomainSearchAnalyzer.java new file mode 100644 index 0000000000..88c1caac40 --- /dev/null +++ b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/DomainSearchAnalyzer.java @@ -0,0 +1,14 @@ +package org.apache.tuscany.sca.domain.search.impl; + +import org.apache.lucene.analysis.PerFieldAnalyzerWrapper; + +public class DomainSearchAnalyzer extends PerFieldAnalyzerWrapper { + + public DomainSearchAnalyzer() { + super(new NamingAnalyzer()); + + addAnalyzer(SearchFields.PARENT_FIELD, new DomainPathAnalyzer()); + + } + +} diff --git a/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/DomainSearchDocumentProcessorsMap.java b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/DomainSearchDocumentProcessorsMap.java new file mode 100644 index 0000000000..66c2898ebd --- /dev/null +++ b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/DomainSearchDocumentProcessorsMap.java @@ -0,0 +1,27 @@ +package org.apache.tuscany.sca.domain.search.impl; + +import org.apache.tuscany.sca.assembly.Binding; +import org.apache.tuscany.sca.assembly.Component; +import org.apache.tuscany.sca.assembly.ComponentType; +import org.apache.tuscany.sca.assembly.Composite; +import org.apache.tuscany.sca.assembly.Property; +import org.apache.tuscany.sca.contribution.Artifact; +import org.apache.tuscany.sca.contribution.Contribution; +import org.apache.tuscany.sca.domain.search.DocumentProcessorsMap; + +public class DomainSearchDocumentProcessorsMap extends DocumentProcessorsMap { + + private static final long serialVersionUID = -4651637686945322606L; + + public DomainSearchDocumentProcessorsMap() { + addDocumentProcessor(Contribution.class, new ContributionDocumentProcessor()); + addDocumentProcessor(Artifact.class, new ArtifactDocumentProcessor()); + addDocumentProcessor(Property.class, new PropertyDocumentProcessor()); + addDocumentProcessor(ComponentType.class, new ComponentTypeDocumentProcessor()); + addDocumentProcessor(Binding.class, new BindingDocumentProcessor()); + addDocumentProcessor(Component.class, new ComponentDocumentProcessor()); + addDocumentProcessor(Composite.class, new CompositeDocumentProcessor()); + + } + +} diff --git a/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/DomainSearchImpl.java b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/DomainSearchImpl.java new file mode 100644 index 0000000000..56b375685b --- /dev/null +++ b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/DomainSearchImpl.java @@ -0,0 +1,140 @@ +package org.apache.tuscany.sca.domain.search.impl; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.List; + +import org.apache.lucene.analysis.Analyzer; +import org.apache.lucene.document.Field; +import org.apache.lucene.index.CorruptIndexException; +import org.apache.lucene.index.IndexWriter; +import org.apache.lucene.queryParser.ParseException; +import org.apache.lucene.queryParser.QueryParser; +import org.apache.lucene.search.HitCollector; +import org.apache.lucene.search.IndexSearcher; +import org.apache.lucene.search.Query; +import org.apache.lucene.store.Directory; +import org.apache.lucene.store.LockObtainFailedException; +import org.apache.lucene.store.RAMDirectory; +import org.apache.tuscany.sca.contribution.Contribution; +import org.apache.tuscany.sca.contribution.service.ContributionRepository; +import org.apache.tuscany.sca.domain.search.DocumentMap; +import org.apache.tuscany.sca.domain.search.DomainSearch; +import org.osoa.sca.annotations.AllowsPassByReference; + +public class DomainSearchImpl implements DomainSearch { + + @AllowsPassByReference + public void contributionAdded(ContributionRepository repository, + Contribution contribution) { + System.out.println("contributionAdded:"); + System.out.println(repository); + System.out.println(contribution); + + try { + Directory dir = new RAMDirectory(); + Analyzer analyzer = new DomainSearchAnalyzer(); + IndexWriter indexWriter = new IndexWriter(dir, analyzer, IndexWriter.MaxFieldLength.UNLIMITED); + DomainSearchDocumentProcessorsMap docProcessors = new DomainSearchDocumentProcessorsMap(); + DocumentMap docs = new DocumentMap(); + + docProcessors.process(docProcessors, docs, contribution, null, ""); + + for (Document doc : docs.values()) { + indexWriter.addDocument(doc.createLuceneDocument()); + } + + indexWriter.close(); + + final IndexSearcher searcher = new IndexSearcher(dir); + BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in)); + QueryParser qp = new QueryParser("", analyzer); + + qp.setAllowLeadingWildcard(true); + + while (true) { + System.out.print("query: "); + String queryString = consoleReader.readLine(); + + if (queryString.equals("exit")) { + break; + } + + try { + Query query = qp.parse(queryString); + + searcher.search(query, new HitCollector() { + + @Override + public void collect(int doc, float score) { + try { + org.apache.lucene.document.Document document = searcher.doc(doc); + List fields = document.getFields(); + System.out.println("---------"); + System.out.println("doc = " + doc); + + for (Object obj : fields) { + Field field = (Field) obj; + String[] values = document.getValues(field.name()); + System.out.println(field); + + for (String value : values) { + System.out.println("\t" + value); + } + + } + + } catch (CorruptIndexException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + }); + + } catch (ParseException e) { + System.out.println(e.getMessage()); + } + + System.out.println(); + + } + + + + } catch (CorruptIndexException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (LockObtainFailedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + + public void contributionRemoved(ContributionRepository repository, + Contribution contribution) { + System.out.println("contributionRemoved:"); + System.out.println(repository); + System.out.println(contribution); + + } + + public void contributionUpdated(ContributionRepository repository, + Contribution oldContribution, Contribution contribution) { + + System.out.println("contributionUpdated:"); + System.out.println(repository); + System.out.println(oldContribution); + System.out.println(contribution); + + } + +} diff --git a/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/DomainSearchModuleActivator.java b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/DomainSearchModuleActivator.java new file mode 100644 index 0000000000..71426717b7 --- /dev/null +++ b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/DomainSearchModuleActivator.java @@ -0,0 +1,20 @@ +package org.apache.tuscany.sca.domain.search.impl; + +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.ModuleActivator; + +public class DomainSearchModuleActivator implements ModuleActivator { + + public DomainSearchModuleActivator() { + // empty constructor + } + + public void start(ExtensionPointRegistry registry) { + registry.addExtensionPoint(new DefaultSearchContributionListenerExtensionPoint()); + } + + public void stop(ExtensionPointRegistry registry) { + // does nothing + } + +} diff --git a/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/FileDocumentProcessor.java b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/FileDocumentProcessor.java new file mode 100644 index 0000000000..27eb52d659 --- /dev/null +++ b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/FileDocumentProcessor.java @@ -0,0 +1,50 @@ +package org.apache.tuscany.sca.domain.search.impl; + +import java.io.File; + +import org.apache.lucene.document.Field; +import org.apache.tuscany.sca.domain.search.DocumentMap; +import org.apache.tuscany.sca.domain.search.DocumentProcessor; +import org.apache.tuscany.sca.domain.search.DocumentProcessorsMap; + +public class FileDocumentProcessor implements DocumentProcessor { + + public Document process(DocumentProcessorsMap processors, + DocumentMap documents, Object object, Document doc, String parent) { + + if (object instanceof File) { + File file = (File) object; + documents.get(file.getAbsoluteFile()); + + if (file.isFile()) { + doc.add(new Field(SearchFields.DIRECTORY_FIELD, file.getAbsolutePath(), + Field.Store.YES, Field.Index.ANALYZED)); + + parent += DomainPathAnalyzer.PATH_SEPARATOR + SearchFields.FILE_FIELD + DomainPathAnalyzer.TYPE_SEPARATOR + file.getName(); + + File[] files = file.listFiles(); + + for (File childFile : files) { + Document fileDoc = processors.process(processors, documents, childFile, null, parent); + + fileDoc.add(new Field(SearchFields.PARENT_FIELD, parent, + Field.Store.YES, Field.Index.ANALYZED)); + + } + + } + + return doc; + + } + + throw new IllegalArgumentException(); + + } + + public Object getDocumentKey(Object object) { + // TODO Auto-generated method stub + return null; + } + +} diff --git a/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/NamingAnalyzer.java b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/NamingAnalyzer.java new file mode 100644 index 0000000000..93e34ee875 --- /dev/null +++ b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/NamingAnalyzer.java @@ -0,0 +1,27 @@ +package org.apache.tuscany.sca.domain.search.impl; + +import java.io.IOException; +import java.io.Reader; + +import org.apache.lucene.analysis.Analyzer; +import org.apache.lucene.analysis.TokenStream; +import org.apache.lucene.analysis.Tokenizer; + +public class NamingAnalyzer extends Analyzer { + + public TokenStream tokenStream(String fieldName, Reader reader) { + return new NamingTokenizer(reader); + } + + public TokenStream reusableTokenStream(String fieldName, Reader reader) + throws IOException { + Tokenizer tokenizer = (Tokenizer) getPreviousTokenStream(); + if (tokenizer == null) { + tokenizer = new NamingTokenizer(reader); + setPreviousTokenStream(tokenizer); + } else + tokenizer.reset(reader); + return tokenizer; + } + +} diff --git a/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/NamingTokenizer.java b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/NamingTokenizer.java new file mode 100644 index 0000000000..0d71b26b3d --- /dev/null +++ b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/NamingTokenizer.java @@ -0,0 +1,127 @@ +package org.apache.tuscany.sca.domain.search.impl; + +import java.io.IOException; +import java.io.Reader; + +import org.apache.lucene.analysis.Token; +import org.apache.lucene.analysis.Tokenizer; + +public class NamingTokenizer extends Tokenizer { + + private int offset = 0, bufferIndex = 0, dataLen = 0; + private static final int MAX_WORD_LEN = 255; + private static final int IO_BUFFER_SIZE = 4096; + private final char[] ioBuffer = new char[IO_BUFFER_SIZE]; + + public NamingTokenizer(Reader reader) { + super(reader); + } + + @Override + public Token next(Token reusableToken) throws IOException { + assert reusableToken != null; + reusableToken.clear(); + int length = 0; + int start = bufferIndex; + char[] buffer = reusableToken.termBuffer(); + + boolean lowercaseCharFound = false; + boolean digitFound = false; + + while (true) { + + if (bufferIndex >= dataLen) { + offset += dataLen; + int incr; + + if (lowercaseCharFound || length == 0) { + incr = 0; + + } else { + incr = 2; + ioBuffer[0] = ioBuffer[bufferIndex - 1]; + ioBuffer[1] = ioBuffer[bufferIndex]; + + } + + dataLen = input.read(ioBuffer, incr, ioBuffer.length - incr); + if (dataLen == -1) { + if (length > 0) + break; + else + return null; + } + bufferIndex = incr; + dataLen += incr; + + } + + final char c = ioBuffer[bufferIndex++]; + boolean breakChar = true; + + if (Character.isDigit(c)) { + + if (digitFound || length == 0) { + breakChar = false; + digitFound = true; + + } else { + bufferIndex--; + } + + // TODO: normalize accent, it does not index accents for now + } else if (c >= 65 && c<= 90 || c >= 97 && c<= 122) { + + if (digitFound) { + bufferIndex--; + + } else if (Character.isLowerCase(c)) { + + if (!(lowercaseCharFound || length <= 1)) { + length--; + bufferIndex -= 2; + + } else { + lowercaseCharFound = true; + breakChar = false; + + } + + } else if (!lowercaseCharFound) { // && uppercase + breakChar = false; + + } else { + bufferIndex--; + } + + } + + if (!breakChar) { + + if (length == 0) // start of token + start = offset + bufferIndex - 1; + else if (length == buffer.length) + buffer = reusableToken.resizeTermBuffer(1 + length); + + buffer[length++] = Character.toLowerCase(c); // buffer it, normalized + + if (length == MAX_WORD_LEN) // buffer overflow! + break; + + } else if (length > 0) {// at non-Letter w/ chars + + + break; // return 'em + + } + + } + + reusableToken.setTermLength(length); + reusableToken.setStartOffset(start); + reusableToken.setEndOffset(start + length); + + return reusableToken; + + } +} diff --git a/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/PropertyDocumentProcessor.java b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/PropertyDocumentProcessor.java new file mode 100644 index 0000000000..0a0ff9ceeb --- /dev/null +++ b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/PropertyDocumentProcessor.java @@ -0,0 +1,63 @@ +package org.apache.tuscany.sca.domain.search.impl; + +import java.lang.reflect.Array; + +import org.apache.lucene.document.Field; +import org.apache.tuscany.sca.assembly.Property; +import org.apache.tuscany.sca.domain.search.DocumentMap; +import org.apache.tuscany.sca.domain.search.DocumentProcessor; +import org.apache.tuscany.sca.domain.search.DocumentProcessorsMap; + +public class PropertyDocumentProcessor implements DocumentProcessor { + + public Document process(DocumentProcessorsMap processors, + DocumentMap documents, Object object, Document doc, String parent) { + + if (object instanceof Property) { + Property property = (Property) object; + String name = property.getName(); + + if (name != null && name.length() > 0) { + + if (doc == null) { + doc = documents.get(name); + } + + Object value = property.getValue(); + + if (value.getClass().isArray()) { + int arraySize = Array.getLength(value); + + for (int i = 0; i < arraySize; i++) { + Object arrayValue = Array.get(value, i); + + doc.add(new Field(SearchFields.VALUE_FIELD, arrayValue.toString(), + Field.Store.YES, Field.Index.ANALYZED)); + + } + + } else { + + doc.add(new Field(SearchFields.VALUE_FIELD, value.toString(), + Field.Store.YES, Field.Index.ANALYZED)); + + } + + return doc == null ? FAKE_DOCUMENT : doc; + + } else { + return FAKE_DOCUMENT; + } + + } + + throw new IllegalArgumentException(); + + } + + public Object getDocumentKey(Object object) { + // TODO Auto-generated method stub + return null; + } + +} diff --git a/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/SearchFields.java b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/SearchFields.java new file mode 100644 index 0000000000..fbb0af8dd8 --- /dev/null +++ b/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/SearchFields.java @@ -0,0 +1,55 @@ +package org.apache.tuscany.sca.domain.search.impl; + +public interface SearchFields { + + final public static String LOCATION_FIELD = "location"; + + final public static String DIRECTORY_FIELD = "directory"; + + final public static String FILE_FIELD = "file"; + + final public static String COMPONENT_TYPE_FIELD = "componenttype"; + + final public static String REFERENCE_FIELD = "reference"; + + final public static String COMPOSITE_FIELD = "composite"; + + final public static String SERVICE_FIELD = "service"; + + final public static String BINDING_FIELD = "binding"; + + final public static String ARTIFACT_FIELD = "artifact"; + + final public static String CONTRIBUTION_FIELD = "contribution"; + + final public static String COMPONENT_FIELD = "component"; + + final public static String TYPE_FIELD = "type"; + + final public static String PARENT_FIELD = "parent"; + + final public static String IMPLEMENTS_FIELD = "implements"; + + final public static String SERVICE_NAME_FIELD = "servicename"; + + final public static String SERVICE_INTERFACE_FIELD = "serviceinterface"; + + final public static String SERVICE_INTERFACE_CALLBACK_FIELD = "serviceinterfacecallback"; + + final public static String REFERENCE_NAME_FIELD = "referencename"; + + final public static String REFERENCE_INTERFACE_FIELD = "referenceinterface"; + + final public static String REFERENCE_INTERFACE_CALLBACK_FIELD = "referenceinterfacecallback"; + + final public static String IMPORTEDBY_FIELD = "importedby"; + + final public static String EXPORTEDBY_FIELD = "exportedby"; + + final public static String INCLUDEDBY_FIELD = "includedby"; + + final public static String KEY_FIELD = "propertykey"; + + final public static String VALUE_FIELD = "propertyvalue"; + +} diff --git a/branches/sca-java-1.x/modules/domain-search/src/test/java/org/apache/tuscany/sca/domain/search/impl/DomainPathAnalyzerTestCase.java b/branches/sca-java-1.x/modules/domain-search/src/test/java/org/apache/tuscany/sca/domain/search/impl/DomainPathAnalyzerTestCase.java new file mode 100644 index 0000000000..54a84f0b49 --- /dev/null +++ b/branches/sca-java-1.x/modules/domain-search/src/test/java/org/apache/tuscany/sca/domain/search/impl/DomainPathAnalyzerTestCase.java @@ -0,0 +1,41 @@ +package org.apache.tuscany.sca.domain.search.impl; + +import java.io.IOException; +import java.io.StringReader; + +import org.apache.lucene.analysis.Token; +import org.apache.lucene.analysis.Tokenizer; + +import junit.framework.TestCase; + +public class DomainPathAnalyzerTestCase extends TestCase { + + private Token reusableToken = new Token(); + + public void test() throws IOException { + + Tokenizer tokenizer = new DomainPathAnalyzer.DomainPathTokenizer(new StringReader( + DomainPathAnalyzer.PATH_SEPARATOR + SearchFields.CONTRIBUTION_FIELD + DomainPathAnalyzer.TYPE_SEPARATOR + "123tuscany" + DomainPathAnalyzer.TYPE_SEPARATOR + "SCA" + + DomainPathAnalyzer.PATH_SEPARATOR + DomainPathAnalyzer.TYPE_SEPARATOR + "TuscanySCA" + DomainPathAnalyzer.TYPE_SEPARATOR + "123")); + + assertNextToken(Character.toString(DomainPathAnalyzer.PATH_SEPARATOR), tokenizer); + assertNextToken("123", tokenizer); + assertNextToken("tuscany", tokenizer); + assertNextToken("sca", tokenizer); + assertNextToken(Character.toString(DomainPathAnalyzer.PATH_SEPARATOR), tokenizer); + assertNextToken("tuscany", tokenizer); + assertNextToken("sca", tokenizer); + assertNextToken("123", tokenizer); + + } + + private void assertNextToken(String expected, Tokenizer tokenizer) + throws IOException { + Token token = tokenizer.next(reusableToken); + + assertNotNull(token); + assertEquals(expected, token.term()); + + } + +} diff --git a/branches/sca-java-1.x/modules/domain-search/src/test/java/org/apache/tuscany/sca/domain/search/impl/NamingTokenizerTestCase.java b/branches/sca-java-1.x/modules/domain-search/src/test/java/org/apache/tuscany/sca/domain/search/impl/NamingTokenizerTestCase.java new file mode 100644 index 0000000000..ea161b48e9 --- /dev/null +++ b/branches/sca-java-1.x/modules/domain-search/src/test/java/org/apache/tuscany/sca/domain/search/impl/NamingTokenizerTestCase.java @@ -0,0 +1,140 @@ +package org.apache.tuscany.sca.domain.search.impl; + +import java.io.IOException; +import java.io.StringReader; + +import org.apache.lucene.analysis.Token; +import org.apache.lucene.analysis.Tokenizer; + +import junit.framework.TestCase; + +public class NamingTokenizerTestCase extends TestCase { + + private Token reusableToken = new Token(); + + public void testDigits() throws IOException { + NamingTokenizer tokenizer = new NamingTokenizer(new StringReader( + "123tuscany")); + + assertNextToken("123", tokenizer); + assertNextToken("tuscany", tokenizer); + + tokenizer.reset(new StringReader("tuscany123")); + + assertNextToken("tuscany", tokenizer); + assertNextToken("123", tokenizer); + + tokenizer.reset(new StringReader("TUSCANY123")); + + assertNextToken("tuscany", tokenizer); + assertNextToken("123", tokenizer); + + tokenizer.reset(new StringReader("123TUSCANY")); + + assertNextToken("123", tokenizer); + assertNextToken("tuscany", tokenizer); + + tokenizer.reset(new StringReader("tuscany.123")); + + assertNextToken("tuscany", tokenizer); + assertNextToken("123", tokenizer); + + tokenizer.reset(new StringReader("123.tuscany")); + + assertNextToken("123", tokenizer); + assertNextToken("tuscany", tokenizer); + + } + + public void testUppercasedTokens() throws IOException { + NamingTokenizer tokenizer = new NamingTokenizer(new StringReader( + "SCATuscany")); + + assertNextToken("sca", tokenizer); + assertNextToken("tuscany", tokenizer); + + tokenizer.reset(new StringReader("TuscanySCA")); + + assertNextToken("tuscany", tokenizer); + assertNextToken("sca", tokenizer); + + tokenizer.reset(new StringReader("Tuscany.SCA")); + + assertNextToken("tuscany", tokenizer); + assertNextToken("sca", tokenizer); + + tokenizer.reset(new StringReader("SCA.Tuscany")); + + assertNextToken("sca", tokenizer); + assertNextToken("tuscany", tokenizer); + + tokenizer.reset(new StringReader("SCA")); + + assertNextToken("sca", tokenizer); + + } + + public void testRegularTokens() throws IOException { + NamingTokenizer tokenizer = new NamingTokenizer(new StringReader( + "scaTuscany")); + + assertNextToken("sca", tokenizer); + assertNextToken("tuscany", tokenizer); + + tokenizer.reset(new StringReader("ScaTuscany")); + + assertNextToken("sca", tokenizer); + assertNextToken("tuscany", tokenizer); + + tokenizer.reset(new StringReader("ScaTuscanY")); + + assertNextToken("sca", tokenizer); + assertNextToken("tuscan", tokenizer); + assertNextToken("y", tokenizer); + + } + + public void testSingleCharTokens() throws IOException { + + NamingTokenizer tokenizer = new NamingTokenizer(new StringReader("1")); + + assertNextToken("1", tokenizer); + + tokenizer.reset(new StringReader("a")); + + assertNextToken("a", tokenizer); + + tokenizer.reset(new StringReader("A")); + + assertNextToken("a", tokenizer); + + } + + public void testNullTokens() throws IOException { + NamingTokenizer tokenizer = new NamingTokenizer(new StringReader("_")); + assertNull(tokenizer.next(this.reusableToken)); + + tokenizer.reset(new StringReader(".")); + assertNull(tokenizer.next(this.reusableToken)); + + tokenizer.reset(new StringReader(" ")); + assertNull(tokenizer.next(this.reusableToken)); + + tokenizer.reset(new StringReader("")); + assertNull(tokenizer.next(this.reusableToken)); + + tokenizer.reset(new StringReader(" )(%*%")); + assertNull(tokenizer.next(this.reusableToken)); + + } + + private void assertNextToken(String expected, Tokenizer tokenizer) + throws IOException { + Token token = tokenizer.next(reusableToken); + + assertNotNull(token); + assertEquals(expected, token.term()); + + } + +} -- cgit v1.2.3