Skip to content

Commit

Permalink
[WIP][ISSUE #482]Add protocol adaptor plugin (#572)
Browse files Browse the repository at this point in the history
* Add protocol adaptor plugin

* ProtocolAdaptor deal with Package

* Remove java doc check
  • Loading branch information
ruanwenjun authored Oct 28, 2021
1 parent cc5b4c9 commit 25c2515
Show file tree
Hide file tree
Showing 19 changed files with 493 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@
* limitations under the License.
*/

//apply plugin: 'java'
////apply plugin: "maven"
//apply plugin: "eclipse"
//apply plugin: "idea"
//
//sourceCompatibility = 1.8

configurations {
implementation.exclude group: 'ch.qos.logback', module: 'logback-classic'
}
Expand Down
29 changes: 29 additions & 0 deletions eventmesh-protocol-plugin/eventmesh-protocol-api/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

dependencies {
implementation project(":eventmesh-spi")
implementation project(":eventmesh-common")

implementation "io.cloudevents:cloudevents-core"

testImplementation project(":eventmesh-spi")
testImplementation project(":eventmesh-common")

testImplementation "io.cloudevents:cloudevents-core"
testImplementation "junit:junit"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.eventmesh.protocol.api;

import org.apache.eventmesh.protocol.api.exception.ProtocolHandleException;
import org.apache.eventmesh.spi.EventMeshExtensionType;
import org.apache.eventmesh.spi.EventMeshSPI;

import io.cloudevents.CloudEvent;
import io.cloudevents.core.v1.CloudEventV1;

/**
* Protocol transformer SPI interface, all protocol plugin should implementation.
*
* <p>All protocol stored in EventMesh is {@link CloudEvent}.
*
* @since 1.3.0
*/
@EventMeshSPI(isSingleton = true, eventMeshExtensionType = EventMeshExtensionType.PROTOCOL)
public interface ProtocolAdaptor {

/**
* transform protocol to {@link CloudEvent}.
*
* @param protocol input protocol
* @return cloud event
*/
CloudEventV1 toCloudEventV1(Package protocol) throws ProtocolHandleException;


/**
* Transform {@link CloudEvent} to target protocol.
*
* @param cloudEvent clout event
* @return target protocol
*/
Package fromCloudEventV1(CloudEventV1 cloudEvent) throws ProtocolHandleException;

/**
* Get protocol type.
*
* @return protocol type, protocol type should not be null
*/
String getProtocolType();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.eventmesh.protocol.api;

import org.apache.eventmesh.spi.EventMeshExtensionFactory;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
* A factory to get Protocol plugin instance.
*
* @since 1.3.0
*/
public enum ProtocolPluginFactory {
;

private static final Map<String, ProtocolAdaptor> PROTOCOL_ADAPTOR_MAP =
new ConcurrentHashMap<>(16);

/**
* Get protocol adaptor by name.
*
* @param protocolType protocol type
* @return protocol adaptor
* @throws IllegalArgumentException if protocol not found
*/
public static ProtocolAdaptor getProtocolAdaptor(String protocolType) {
ProtocolAdaptor protocolAdaptor = PROTOCOL_ADAPTOR_MAP.computeIfAbsent(
protocolType,
(type) -> EventMeshExtensionFactory.getExtension(ProtocolAdaptor.class, type)
);
if (protocolAdaptor == null) {
throw new IllegalArgumentException(
String.format("Cannot find the Protocol adaptor: %s", protocolType)
);
}
return protocolAdaptor;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.eventmesh.protocol.api.exception;

/**
* Protocol Handle exception, will be thrown in protocol handling.
*
* @since 1.3.0
*/
public class ProtocolHandleException extends Exception {

public ProtocolHandleException(String message) {
super(message);
}

public ProtocolHandleException(String message, Throwable cause) {
super(message, cause);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

dependencies {
compileOnly project(":eventmesh-protocol-plugin:eventmesh-protocol-api")
implementation "io.cloudevents:cloudevents-core"

testImplementation project(":eventmesh-protocol-plugin:eventmesh-protocol-api")
testImplementation "io.cloudevents:cloudevents-core"
testImplementation "junit:junit"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.eventmesh.protocol.cloudevents;

import org.apache.eventmesh.protocol.api.ProtocolAdaptor;

import io.cloudevents.core.v1.CloudEventV1;

/**
* CloudEvents protocol adaptor, used to transform CloudEvents message to CloudEvents message.
*
* @since 1.3.0
*/
public class CloudEventsProtocolAdaptor implements ProtocolAdaptor {

@Override
public CloudEventV1 toCloudEventV1(Package cloudEvent) {
return null;
}

@Override
public Package fromCloudEventV1(CloudEventV1 cloudEvent) {
return null;
}

@Override
public String getProtocolType() {
return CloudEventsProtocolConstant.PROTOCOL_NAME;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.eventmesh.protocol.cloudevents;

public enum CloudEventsProtocolConstant {
;
public static final String PROTOCOL_NAME = "cloudevents";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

cloudevents=org.apache.eventmesh.protocol.cloudevents.CloudEventsProtocolAdaptor
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.eventmesh.protocol.cloudevents;

import org.apache.eventmesh.protocol.api.ProtocolAdaptor;
import org.apache.eventmesh.protocol.api.ProtocolPluginFactory;

import org.junit.Assert;
import org.junit.Test;

public class CloudEventsProtocolAdaptorTest {

@Test
public void loadPlugin() {
ProtocolAdaptor protocolAdaptor =
ProtocolPluginFactory.getProtocolAdaptor(CloudEventsProtocolConstant.PROTOCOL_NAME);

Assert.assertNotNull(protocolAdaptor);
Assert.assertEquals(
CloudEventsProtocolConstant.PROTOCOL_NAME, protocolAdaptor.getProtocolType());
Assert.assertEquals(CloudEventsProtocolAdaptor.class, protocolAdaptor.getClass());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

dependencies {
compileOnly project(":eventmesh-protocol-plugin:eventmesh-protocol-api")
implementation "io.cloudevents:cloudevents-core"
implementation "io.openmessaging:openmessaging-api"

testImplementation project(":eventmesh-protocol-plugin:eventmesh-protocol-api")
testImplementation "io.cloudevents:cloudevents-core"
testImplementation "io.openmessaging:openmessaging-api"
testImplementation "junit:junit"
}
Loading

0 comments on commit 25c2515

Please sign in to comment.