forked from micw/php-java-bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContextServer.java
More file actions
149 lines (132 loc) · 6.12 KB
/
Copy pathContextServer.java
File metadata and controls
149 lines (132 loc) · 6.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*-*- mode: Java; tab-width:8 -*-*/
package php.java.bridge.http;
/*
* Copyright (C) 2003-2007 Jost Boekemeier
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
import php.java.bridge.AppThreadPool;
import php.java.bridge.ILogger;
import php.java.bridge.Util;
/**
* A bridge pattern which either uses the PipeContextServer or the SocketContextServer,
* depending on the OS and/or the security restrictions. On windows, which cannot use named pipes,
* a SocketContextServer is used. All other operating systems use a PipeContextServer unless the
* system property php.java.bridge.promiscuous is set to true or the system property
* php.java.bridge.no_pipe_server is set to true.
* <p>
* A ContextServer instance represents the current web context.
* When the PipeContextServer is used, there can be more than one PipeContextServer instance per classloader, the ContextFactory.get() checks
* if it is called with the same ContextServer and throws a SecurityException otherwise. So one cannot access contexts belonging to other web contexts.
* </p><p>
* The SocketContextServer uses only one server socket for all shared web contexts and cannot do any security checks.
* </p>
* @author jostb
* @see php.java.bridge.http.SocketContextServer
*/
public final class ContextServer {
private String contextName;
private boolean promiscuous;
/** Only for internal use */
public static final String ROOT_CONTEXT_SERVER_ATTRIBUTE = ContextServer.class.getName()+".ROOT";
// There's only one shared SocketContextServer instance, otherwise we have to allocate a new ServerSocket for each web context
private static SocketContextServer sock = null;
// One pool for both, the Socket- and the PipeContextServer
private static AppThreadPool pool;
private static synchronized AppThreadPool getAppThreadPool() {
if (pool!=null) return pool;
return pool = new AppThreadPool("JavaBridgeContextRunner", Integer.parseInt(Util.THREAD_POOL_MAX_SIZE));
}
private class SocketChannelName extends AbstractChannelName {
public SocketChannelName(String name,IContextFactory ctx) {super(name, ctx);}
public boolean startChannel(ILogger logger) {
return sock.start(this, logger);
}
public String toString() {
return "Socket:"+getName();
}
}
/**
* Create a new ContextServer using a thread pool.
* @param contextName The the name of the web context to which this server belongs.
*/
public ContextServer(String contextName, boolean promiscuous) {
this.contextName = contextName;
this.promiscuous = promiscuous;
/* socket context server will be created on demand */
}
/**
* @return true for all network interfaces, false for loopback only
*
*/
public boolean isPromiscuous () {
return this.promiscuous;
}
private synchronized static final void destroyContextServer () {
if(sock!=null) sock.destroy();
sock = null;
ContextFactory.destroyAll();
php.java.bridge.SessionFactory.destroyTimer();
if(pool!=null) pool.destroy();
pool = null;
}
/**
* Destroy the pipe or socket context server.
*/
public void destroy() {
destroyContextServer();
}
/**
* Check if either the pipe of the socket context server is available. This function
* may try start a SocketContextServer, if a PipeContextServer is not available.
* @param channelName The header value for X_JAVABRIDGE_CHANNEL, may be null.
* @return true if either the pipe or the socket context server is available.
*/
public boolean isAvailable(String channelName) {
if(!SocketContextServer.SOCKET_SERVER_AVAIL) return false;
SocketContextServer sock=getSocketContextServer(this, getAppThreadPool(), contextName);
return sock!=null && sock.isAvailable();
}
private static synchronized SocketContextServer getSocketContextServer(ContextServer server, AppThreadPool pool, String contextName) {
if(sock!=null) return sock;
return sock=new SocketContextServer(pool, server.isPromiscuous(), contextName);
}
/**
* Start a channel name.
* @param channelName The ChannelName.
* @throws IllegalStateException if there's no Pipe- or SocketContextServer available
*/
public void start(AbstractChannelName channelName, ILogger logger) {
boolean started = channelName.start(logger);
if(!started) throw new IllegalStateException("SocketContextServer not available");
}
/**
* Return the channelName which be passed to the client as X_JAVABRIDGE_REDIRECT
* @param currentCtx The current ContextFactory, see X_JAVABRIDGE_CONTEXT
* @return The channel name of the Pipe- or SocketContextServer.
*/
public AbstractChannelName getChannelName(IContextFactory currentCtx) {
SocketContextServer sock=getSocketContextServer(this, getAppThreadPool(), contextName);
return sock.isAvailable() ? new SocketChannelName(sock.getChannelName(), currentCtx) : null;
}
/**{@inheritDoc}*/
public String toString () {
return "ContextServer: " + contextName;
}
}