Skip to content

Commit 7a405bb

Browse files
committed
Python foreign provider
1 parent 61f7e3d commit 7a405bb

33 files changed

+912
-1
lines changed

biz.aQute.api/bnd.bnd

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
org.osgi.annotation.bundle,\
1010
org.osgi.dto,\
1111
org.apache.felix.http.servlet-api,\
12-
org.osgi.service.component.annotations
12+
org.osgi.service.component.annotations,\
13+
org.osgi.namespace.extender
1314

1415
-sub: *.bnd

biz.aQute.api/kpi.bnd

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-includepackage: biz.aQute.kpi.api

biz.aQute.api/python.bnd

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-includepackage: biz.aQute.foreign.python.api
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package biz.aQute.foreign.python.api;
2+
3+
public interface ForeignPythonConstants {
4+
String EXTENDER_NAME = "biz.aQute.foreign.python";
5+
String VERSION = "1.0.0";
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package biz.aQute.foreign.python.api;
2+
3+
import java.lang.annotation.Documented;
4+
import java.lang.annotation.ElementType;
5+
import java.lang.annotation.Retention;
6+
import java.lang.annotation.RetentionPolicy;
7+
import java.lang.annotation.Target;
8+
9+
import org.osgi.annotation.bundle.Capability;
10+
import org.osgi.namespace.extender.ExtenderNamespace;
11+
12+
@Documented
13+
@Retention(RetentionPolicy.CLASS)
14+
@Target({
15+
ElementType.TYPE, ElementType.PACKAGE
16+
})
17+
@Capability(namespace = ExtenderNamespace.EXTENDER_NAMESPACE, name=ForeignPythonConstants.EXTENDER_NAME, version=ForeignPythonConstants.VERSION)
18+
public @interface ProvideForeignPython {
19+
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package biz.aQute.foreign.python.api;
2+
3+
import java.lang.annotation.Documented;
4+
import java.lang.annotation.ElementType;
5+
import java.lang.annotation.Retention;
6+
import java.lang.annotation.RetentionPolicy;
7+
import java.lang.annotation.Target;
8+
9+
import org.osgi.annotation.bundle.Requirement;
10+
import org.osgi.namespace.extender.ExtenderNamespace;
11+
12+
@Documented
13+
@Retention(RetentionPolicy.CLASS)
14+
@Target({
15+
ElementType.TYPE, ElementType.PACKAGE
16+
})
17+
@Requirement(namespace = ExtenderNamespace.EXTENDER_NAMESPACE, name = ForeignPythonConstants.EXTENDER_NAME, version=ForeignPythonConstants.VERSION)
18+
public @interface RequireForeignPython {
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@Version("1.0.0")
2+
@org.osgi.annotation.bundle.Export
3+
package biz.aQute.foreign.python.api;
4+
5+
import org.osgi.annotation.versioning.Version;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package biz.aQute.kpi.api;
2+
3+
import org.osgi.dto.DTO;
4+
5+
/**
6+
* A Key Performance Indicator (KPI) is a set of values representing the ongoing state of a service implementation.
7+
* <p>
8+
* A service implementation can define a Data Transfer Object (DTO) that contains its measurements. This services
9+
* van ask for a snapshot of this value. The caller then owns the returned object. The DTO can also be reset.
10+
*
11+
*/
12+
public interface KPI {
13+
14+
/**
15+
* Reset any counters and time based values
16+
*/
17+
void reset();
18+
19+
/**
20+
* Return a snapshot
21+
*
22+
* @return a snapshot of the KPI values
23+
*/
24+
DTO snapshot();
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@org.osgi.annotation.bundle.Export
2+
@Version("1.0.0")
3+
package biz.aQute.kpi.api;
4+
5+
import org.osgi.annotation.versioning.Version;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
4+
<classpathentry kind="con" path="aQute.bnd.classpath.container"/>
5+
<classpathentry kind="src" output="target/classes" path="src/main/java"/>
6+
<classpathentry kind="src" output="target/classes" path="src/main/resources"/>
7+
<classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
8+
<classpathentry kind="output" path="bin"/>
9+
</classpath>
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>biz.aQute.foreign.python.provider</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.python.pydev.PyDevBuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.jdt.core.javabuilder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
<buildCommand>
19+
<name>bndtools.core.bndbuilder</name>
20+
<arguments>
21+
</arguments>
22+
</buildCommand>
23+
</buildSpec>
24+
<natures>
25+
<nature>org.eclipse.jdt.core.javanature</nature>
26+
<nature>bndtools.core.bndnature</nature>
27+
<nature>org.python.pydev.pythonNature</nature>
28+
</natures>
29+
</projectDescription>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<?eclipse-pydev version="1.0"?><pydev_project>
3+
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Default</pydev_property>
4+
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python interpreter</pydev_property>
5+
</pydev_project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
eclipse.preferences.version=1
2+
encoding/bnd.bnd=UTF-8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.8
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.release=disabled
12+
org.eclipse.jdt.core.compiler.source=1.8
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
-buildpath: \
2+
org.osgi.service.component.annotations,\
3+
org.osgi.util.tracker,\
4+
org.osgi.framework,\
5+
aQute.libg,\
6+
biz.aQute.api.scheduler,\
7+
biz.aQute.api.python,\
8+
slf4j.api,\
9+
org.osgi.annotation.bundle,\
10+
osgi.annotation,\
11+
org.apache.felix.gogo.runtime,\
12+
org.osgi.dto,\
13+
biz.aQute.api.kpi
14+
15+
-testpath: \
16+
biz.aQute.launchpad,\
17+
biz.aQute.wrapper.junit,\
18+
biz.aQute.wrapper.hamcrest,\
19+
osgi.core,\
20+
slf4j.simple,\
21+
org.assertj.core,\
22+
biz.aQute.osgi.configuration.util
23+
24+
25+
-conditionalpackage: aQute.*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import sys
2+
3+
print("Error", file=sys.stderr)
4+
5+
raise Exception("Some error")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import time
2+
import sys
3+
4+
print("forever",file=sys.stderr)
5+
sys.stderr.flush()
6+
7+
while True:
8+
print(".",file=sys.stderr)
9+
sys.stderr.flush()
10+
time.sleep(0.25)
11+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import json
2+
import sys
3+
4+
class Gogo():
5+
def __init__(self, instream, outstream):
6+
self.instream = instream
7+
self.outstream = outstream
8+
def command(self, *args):
9+
self.outstream.write( " ".join(args)+"\n")
10+
self.outstream.flush()
11+
json_data = self.instream.readline()
12+
print("read " + json_data,file=sys.stderr)
13+
sys.stderr.flush()
14+
while json_data == None or json_data == "":
15+
print("got none " + json_data,file=sys.stderr)
16+
sys.stderr.flush()
17+
json_data = self.instream.readline()
18+
data = json.loads(json_data)
19+
return data
20+
21+
22+
23+
g = Gogo(sys.stdin, sys.stdout)
24+
print("start python",file=sys.stderr)
25+
sys.stderr.flush()
26+
27+
while True:
28+
r = g.command("test")
29+
assert r['value'] == 42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import sys
2+
3+
print("Hello World", file=sys.stderr)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package biz.aQute.foreign.python.configuration;
2+
3+
public @interface Configuration {
4+
String python() default "python3";
5+
long restartDelay() default 5000;
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@org.osgi.annotation.bundle.Export
2+
@Version("1.0.0")
3+
package biz.aQute.foreign.python.configuration;
4+
5+
import org.osgi.annotation.versioning.Version;
6+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package biz.aQute.foreign.python.provider;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
public class About {
7+
final static Logger logger = LoggerFactory.getLogger("biz.aQute.foreign.python");
8+
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package biz.aQute.foreign.python.provider;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.io.InterruptedIOException;
6+
7+
public class Exchange extends InputStream implements Appendable {
8+
final static int MASK = 0x1FFF;
9+
char buffer[] = new char[MASK + 1];
10+
int available = buffer.length;
11+
int in, out;
12+
int count = 0;
13+
int codepoint;
14+
final String name;
15+
16+
public Exchange(String name) {
17+
this.name = name;
18+
}
19+
20+
@Override
21+
public Appendable append(CharSequence csq) throws IOException {
22+
return append(csq, 0, csq.length());
23+
}
24+
25+
@Override
26+
public Appendable append(CharSequence csq, int start, int end) throws IOException {
27+
for (int i = start; i < end; i++)
28+
append(csq.charAt(i));
29+
return this;
30+
}
31+
32+
@Override
33+
public synchronized Appendable append(char c) throws IOException {
34+
while (available == 0) {
35+
try {
36+
wait();
37+
} catch (InterruptedException e) {
38+
throw new InterruptedIOException();
39+
}
40+
}
41+
available--;
42+
buffer[in] = c;
43+
in = (in + 1) & MASK;
44+
notifyAll();
45+
return this;
46+
}
47+
48+
public synchronized char readChar() throws InterruptedIOException {
49+
while (in == out) {
50+
try {
51+
wait();
52+
} catch (InterruptedException e) {
53+
throw new InterruptedIOException();
54+
}
55+
}
56+
char c = buffer[out];
57+
out = (out + 1) & MASK;
58+
available++;
59+
return c;
60+
}
61+
62+
@Override
63+
public int read() throws IOException {
64+
if (count-- > 0) {
65+
int r = codepoint >> (count * 6);
66+
r &= 0b0011_1111;
67+
r |= 0b1000_0000;
68+
return r;
69+
}
70+
71+
codepoint = readChar();
72+
if (codepoint < 0)
73+
return -1;
74+
if (codepoint > 0xFFFF)
75+
return 0;
76+
77+
if (codepoint < 0x80)
78+
return codepoint;
79+
80+
if (codepoint < 0x800) {
81+
count = 1;
82+
int v = (codepoint >> 6) | 0b1100_0000;
83+
return v;
84+
}
85+
count = 2;
86+
int v = (codepoint >> 12) | 0b1110_0000;
87+
return v;
88+
}
89+
90+
public synchronized int available() {
91+
return available;
92+
}
93+
94+
public synchronized String toString() {
95+
StringBuilder sb = new StringBuilder(name).append(": ");
96+
int i=out;
97+
while ( i!=out) {
98+
sb.append(buffer[i]);
99+
i = (i+1) & MASK;
100+
}
101+
return sb.toString();
102+
}
103+
}

0 commit comments

Comments
 (0)