Skip to content

Commit 2f42bf9

Browse files
committed
Work in progress with contexts.
1 parent 8bbf6be commit 2f42bf9

17 files changed

+519
-12
lines changed

src/fbjava-tests/src/main/java/example/Functions.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,20 @@
1818
*/
1919
package example;
2020

21+
import java.lang.reflect.Method;
2122
import java.math.BigDecimal;
2223
import java.security.AccessController;
2324
import java.security.PrivilegedAction;
25+
import java.security.PrivilegedExceptionAction;
2426
import java.sql.Blob;
2527
import java.sql.Connection;
2628
import java.sql.DriverManager;
2729
import java.sql.ResultSet;
2830
import java.sql.SQLException;
2931
import java.sql.Statement;
3032

33+
import org.firebirdsql.fbjava.Context;
34+
3135

3236
public class Functions
3337
{
@@ -225,4 +229,33 @@ public static int f26()
225229
return 0;
226230
}
227231
}
232+
233+
public static int f25() throws Exception
234+
{
235+
AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() {
236+
@Override
237+
public Void run() throws Exception
238+
{
239+
Class<?> clazz = Class.forName("org.firebirdsql.fbjava.impl.InternalContext");
240+
Method method = clazz.getMethod("get");
241+
method.setAccessible(true);
242+
method.invoke(null);
243+
return null;
244+
}
245+
});
246+
247+
return 0;
248+
}
249+
250+
public static String f26()
251+
{
252+
Context context = Context.get();
253+
return context.getPackageName() + "." + context.getObjectName() + "!" + context.getNameInfo();
254+
}
255+
256+
public static String f27()
257+
{
258+
Context context = Context.get();
259+
return context.getBody();
260+
}
228261
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* FB/Java plugin
3+
*
4+
* Distributable under LGPL license.
5+
* You may obtain a copy of the License at http://www.gnu.org/copyleft/lgpl.html
6+
*
7+
* This program is distributed in the hope that it will be useful,
8+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
* LGPL License for more details.
11+
*
12+
* This file was created by members of the Firebird development team.
13+
* All individual contributions remain the Copyright (C) of those
14+
* individuals. Contributors to this file are either listed here or
15+
* can be obtained from a git log command.
16+
*
17+
* All rights reserved.
18+
*/
19+
package org.firebirdsql.fbjava;
20+
21+
22+
/**
23+
* This interface represents a Firebird Function or Procedure Context.
24+
*
25+
* @author <a href="mailto:adrianosf@gmail.com">Adriano dos Santos Fernandes</a>
26+
*/
27+
public interface CallableRoutineContext extends Context
28+
{
29+
/**
30+
* Gets the Context instance associated with the current call.
31+
*/
32+
public static CallableRoutineContext get()
33+
{
34+
return (CallableRoutineContext) Context.get();
35+
}
36+
37+
/**
38+
* Gets the metadata package name that called the external routine.
39+
* For unpackaged routines, return null.
40+
*/
41+
public String getPackageName();
42+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* FB/Java plugin
3+
*
4+
* Distributable under LGPL license.
5+
* You may obtain a copy of the License at http://www.gnu.org/copyleft/lgpl.html
6+
*
7+
* This program is distributed in the hope that it will be useful,
8+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
* LGPL License for more details.
11+
*
12+
* This file was created by members of the Firebird development team.
13+
* All individual contributions remain the Copyright (C) of those
14+
* individuals. Contributors to this file are either listed here or
15+
* can be obtained from a git log command.
16+
*
17+
* All rights reserved.
18+
*/
19+
package org.firebirdsql.fbjava;
20+
21+
import java.lang.reflect.Method;
22+
import java.security.AccessController;
23+
import java.security.PrivilegedAction;
24+
import java.sql.Connection;
25+
import java.sql.SQLException;
26+
27+
28+
/**
29+
* This interface represents a Firebird External Context.
30+
*
31+
* @author <a href="mailto:adrianosf@gmail.com">Adriano dos Santos Fernandes</a>
32+
*/
33+
public interface Context
34+
{
35+
/**
36+
* Gets the Context instance associated with the current call.
37+
*/
38+
public static Context get()
39+
{
40+
// For security purposes, InternalContextImpl is not public, so get it by reflection.
41+
42+
return AccessController.doPrivileged(new PrivilegedAction<Context>() {
43+
@Override
44+
public Context run()
45+
{
46+
try
47+
{
48+
Class<?> clazz = Class.forName("org.firebirdsql.fbjava.impl.InternalContext");
49+
Method method = clazz.getMethod("getContextImpl");
50+
method.setAccessible(true);
51+
return (Context) method.invoke(null);
52+
}
53+
catch (Exception e)
54+
{
55+
throw new RuntimeException(e);
56+
}
57+
}
58+
});
59+
}
60+
61+
/**
62+
* Gets the Connection object.
63+
*/
64+
public Connection getConnection() throws SQLException;
65+
66+
/**
67+
* Gets the metadata object name that called the external routine.
68+
*/
69+
public String getObjectName();
70+
71+
/**
72+
* Gets info stored at entry point metadata.
73+
* TODO: Document how this can be used.
74+
*/
75+
public String getNameInfo();
76+
77+
/**
78+
* Gets metadata body.
79+
*/
80+
public String getBody();
81+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* FB/Java plugin
3+
*
4+
* Distributable under LGPL license.
5+
* You may obtain a copy of the License at http://www.gnu.org/copyleft/lgpl.html
6+
*
7+
* This program is distributed in the hope that it will be useful,
8+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
* LGPL License for more details.
11+
*
12+
* This file was created by members of the Firebird development team.
13+
* All individual contributions remain the Copyright (C) of those
14+
* individuals. Contributors to this file are either listed here or
15+
* can be obtained from a git log command.
16+
*
17+
* All rights reserved.
18+
*/
19+
package org.firebirdsql.fbjava;
20+
21+
22+
/**
23+
* This interface represents a Firebird Function Context.
24+
*
25+
* @author <a href="mailto:adrianosf@gmail.com">Adriano dos Santos Fernandes</a>
26+
*/
27+
public interface FunctionContext extends CallableRoutineContext
28+
{
29+
/**
30+
* Gets the Context instance associated with the current call.
31+
*/
32+
public static FunctionContext get()
33+
{
34+
return (FunctionContext) Context.get();
35+
}
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* FB/Java plugin
3+
*
4+
* Distributable under LGPL license.
5+
* You may obtain a copy of the License at http://www.gnu.org/copyleft/lgpl.html
6+
*
7+
* This program is distributed in the hope that it will be useful,
8+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
* LGPL License for more details.
11+
*
12+
* This file was created by members of the Firebird development team.
13+
* All individual contributions remain the Copyright (C) of those
14+
* individuals. Contributors to this file are either listed here or
15+
* can be obtained from a git log command.
16+
*
17+
* All rights reserved.
18+
*/
19+
package org.firebirdsql.fbjava;
20+
21+
22+
/**
23+
* This interface represents a Firebird Procedure Context.
24+
*
25+
* @author <a href="mailto:adrianosf@gmail.com">Adriano dos Santos Fernandes</a>
26+
*/
27+
public interface ProcedureContext extends CallableRoutineContext
28+
{
29+
/**
30+
* Gets the Context instance associated with the current call.
31+
*/
32+
public static ProcedureContext get()
33+
{
34+
return (ProcedureContext) Context.get();
35+
}
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* FB/Java plugin
3+
*
4+
* Distributable under LGPL license.
5+
* You may obtain a copy of the License at http://www.gnu.org/copyleft/lgpl.html
6+
*
7+
* This program is distributed in the hope that it will be useful,
8+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
* LGPL License for more details.
11+
*
12+
* This file was created by members of the Firebird development team.
13+
* All individual contributions remain the Copyright (C) of those
14+
* individuals. Contributors to this file are either listed here or
15+
* can be obtained from a git log command.
16+
*
17+
* All rights reserved.
18+
*/
19+
package org.firebirdsql.fbjava;
20+
21+
22+
/**
23+
* This interface represents a Firebird Trigger Context.
24+
*
25+
* @author <a href="mailto:adrianosf@gmail.com">Adriano dos Santos Fernandes</a>
26+
*/
27+
public interface TriggerContext extends Context
28+
{
29+
/**
30+
* Gets the Context instance associated with the current call.
31+
*/
32+
public static TriggerContext get()
33+
{
34+
return (TriggerContext) Context.get();
35+
}
36+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* FB/Java plugin
3+
*
4+
* Distributable under LGPL license.
5+
* You may obtain a copy of the License at http://www.gnu.org/copyleft/lgpl.html
6+
*
7+
* This program is distributed in the hope that it will be useful,
8+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
* LGPL License for more details.
11+
*
12+
* This file was created by members of the Firebird development team.
13+
* All individual contributions remain the Copyright (C) of those
14+
* individuals. Contributors to this file are either listed here or
15+
* can be obtained from a git log command.
16+
*
17+
* All rights reserved.
18+
*/
19+
package org.firebirdsql.fbjava.impl;
20+
21+
import org.firebirdsql.fbjava.CallableRoutineContext;
22+
23+
24+
abstract class CallableRoutineContextImpl extends ContextImpl implements CallableRoutineContext
25+
{
26+
private InternalContext internalContext;
27+
28+
//// TODO: input/output parameters
29+
30+
public CallableRoutineContextImpl(InternalContext internalContext)
31+
{
32+
super(internalContext);
33+
}
34+
35+
@Override
36+
public String getPackageName()
37+
{
38+
return internalContext.getRoutine().packageName;
39+
}
40+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* FB/Java plugin
3+
*
4+
* Distributable under LGPL license.
5+
* You may obtain a copy of the License at http://www.gnu.org/copyleft/lgpl.html
6+
*
7+
* This program is distributed in the hope that it will be useful,
8+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
* LGPL License for more details.
11+
*
12+
* This file was created by members of the Firebird development team.
13+
* All individual contributions remain the Copyright (C) of those
14+
* individuals. Contributors to this file are either listed here or
15+
* can be obtained from a git log command.
16+
*
17+
* All rights reserved.
18+
*/
19+
package org.firebirdsql.fbjava.impl;
20+
21+
import java.sql.Connection;
22+
import java.sql.SQLException;
23+
24+
import org.firebirdsql.fbjava.Context;
25+
26+
27+
abstract class ContextImpl implements Context
28+
{
29+
private InternalContext internalContext;
30+
31+
public ContextImpl(InternalContext internalContext)
32+
{
33+
this.internalContext = internalContext;
34+
}
35+
36+
@Override
37+
public Connection getConnection() throws SQLException
38+
{
39+
return internalContext.getConnection();
40+
}
41+
42+
@Override
43+
public String getObjectName()
44+
{
45+
return internalContext.getRoutine().objectName;
46+
}
47+
48+
@Override
49+
public String getNameInfo()
50+
{
51+
return internalContext.getRoutine().nameInfo;
52+
}
53+
54+
@Override
55+
public String getBody()
56+
{
57+
return internalContext.getRoutine().body;
58+
}
59+
}

0 commit comments

Comments
 (0)