|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2007 java2script.org and others. |
| 3 | + * All rights reserved. This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * http://www.eclipse.org/legal/epl-v10.html |
| 7 | + * |
| 8 | + * Contributors: |
| 9 | + * Zhou Renjian - initial API and implementation |
| 10 | + *******************************************************************************/ |
| 11 | +package net.sf.j2s.ajax; |
| 12 | + |
| 13 | +import java.util.Collections; |
| 14 | +import java.util.HashMap; |
| 15 | +import java.util.Map; |
| 16 | +import java.util.Vector; |
| 17 | + |
| 18 | +import net.sf.j2s.ajax.SimpleSerializable; |
| 19 | + |
| 20 | +/** |
| 21 | + * |
| 22 | + * @author zhou renjian |
| 23 | + */ |
| 24 | +class SimplePipeHelper { |
| 25 | + |
| 26 | + static interface IPipeThrough { |
| 27 | + public void helpThrough(SimplePipeRunnable pipe, SimpleSerializable[] objs); |
| 28 | + } |
| 29 | + |
| 30 | + private static Map<String, Vector<SimpleSerializable>> pipeMap = null; |
| 31 | + |
| 32 | + private SimplePipeHelper() { |
| 33 | + // |
| 34 | + } |
| 35 | + |
| 36 | + private static Map<String, SimplePipeRunnable> pipes; |
| 37 | + |
| 38 | + /** |
| 39 | + * |
| 40 | + * @param key |
| 41 | + * @param pipe |
| 42 | + * |
| 43 | + * @j2sNative |
| 44 | + * if (key == null || pipe == null) return; |
| 45 | + * if (net.sf.j2s.ajax.SimplePipeHelper.pipes == null) { |
| 46 | + * net.sf.j2s.ajax.SimplePipeHelper.pipes = new Object (); |
| 47 | + * } |
| 48 | + * net.sf.j2s.ajax.SimplePipeHelper.pipes[key] = pipe; |
| 49 | + */ |
| 50 | + public static void registerPipe(String key, SimplePipeRunnable pipe) { |
| 51 | + if (key == null || pipe == null) return; |
| 52 | + if (pipes == null) { |
| 53 | + pipes = Collections.synchronizedMap(new HashMap<String, SimplePipeRunnable>(50)); |
| 54 | + } |
| 55 | + pipes.put(key, pipe); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * |
| 60 | + * @param pipe |
| 61 | + * @return |
| 62 | + * @j2sIgnore |
| 63 | + */ |
| 64 | + static String registerPipe(SimplePipeRunnable pipe) { |
| 65 | + if (pipe == null) return null; |
| 66 | + if (pipes == null) { |
| 67 | + pipes = Collections.synchronizedMap(new HashMap<String, SimplePipeRunnable>(50)); |
| 68 | + } |
| 69 | + String key = nextPipeKey(); |
| 70 | + while (pipes.get(key) != null) { |
| 71 | + key = nextPipeKey();; |
| 72 | + } |
| 73 | + pipes.put(key, pipe); |
| 74 | + |
| 75 | + if (pipeMap == null) { |
| 76 | + pipeMap = Collections.synchronizedMap(new HashMap<String, Vector<SimpleSerializable>>()); |
| 77 | + } |
| 78 | + Vector<SimpleSerializable> vector = pipeMap.get(key); |
| 79 | + if (vector == null) { |
| 80 | + vector = new Vector<SimpleSerializable>(); |
| 81 | + pipeMap.put(key, vector); |
| 82 | + } |
| 83 | + |
| 84 | + return key; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Generate random pipe key. |
| 89 | + * @return |
| 90 | + * |
| 91 | + * @j2sIgnore |
| 92 | + */ |
| 93 | + private static String nextPipeKey() { |
| 94 | + StringBuffer hexString = new StringBuffer(); |
| 95 | + while (hexString.length() < 32) { |
| 96 | + long rnd = Math.abs(Math.round(Long.MAX_VALUE * Math.random())); |
| 97 | + hexString.append(Long.toHexString(rnd)); |
| 98 | + } |
| 99 | + return hexString.substring(0, 32); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * |
| 104 | + * @param key |
| 105 | + * |
| 106 | + * @j2sNative |
| 107 | + * delete net.sf.j2s.ajax.SimplePipeHelper.pipes[key]; |
| 108 | + */ |
| 109 | + static void removePipe(String key) { |
| 110 | + if (pipes == null || key == null) return; |
| 111 | + SimplePipeRunnable removedPipe = pipes.remove(key); |
| 112 | + if (removedPipe != null) { |
| 113 | + if (pipeMap != null) { |
| 114 | + Vector<SimpleSerializable> removedVector = pipeMap.remove(key); |
| 115 | + if (removedVector != null) { |
| 116 | + synchronized (removedVector) { |
| 117 | + removedVector.notifyAll(); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * |
| 126 | + * @param key |
| 127 | + * @return |
| 128 | + * |
| 129 | + * @j2sNative |
| 130 | + * return net.sf.j2s.ajax.SimplePipeHelper.pipes[key]; |
| 131 | + */ |
| 132 | + static SimplePipeRunnable getPipe(String key) { |
| 133 | + if (pipes == null || key == null) return null; |
| 134 | + return pipes.get(key); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * |
| 139 | + * @param key |
| 140 | + * @return |
| 141 | + * @j2sIgnore |
| 142 | + */ |
| 143 | + static Vector<SimpleSerializable> getPipeVector(String key) { |
| 144 | + if (pipeMap == null) { |
| 145 | + return null; |
| 146 | + } |
| 147 | + return pipeMap.get(key); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Tear down the pipe and release its resources. |
| 152 | + * @param key |
| 153 | + * @j2sIgnore |
| 154 | + */ |
| 155 | + static void pipeTearDown(String key) { |
| 156 | + if (pipeMap == null) return; |
| 157 | + Vector<SimpleSerializable> vector = pipeMap.get(key); |
| 158 | + if (vector == null) return; |
| 159 | + vector.add(null); // terminating signal |
| 160 | + synchronized (vector) { |
| 161 | + //System.out.println("Tear down..."); |
| 162 | + vector.notifyAll(); |
| 163 | + } |
| 164 | + pipeMap.remove(key); |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Pipe in datum. |
| 169 | + * |
| 170 | + * @param key |
| 171 | + * @param ss |
| 172 | + * @j2sIgnore |
| 173 | + */ |
| 174 | + static void pipeIn(String key, SimpleSerializable[] ss) { |
| 175 | + Vector<SimpleSerializable> vector = getPipeVector(key); |
| 176 | + if (vector == null) return; // throw exception? |
| 177 | + for (int i = 0; i < ss.length; i++) { |
| 178 | + vector.add(ss[i]); |
| 179 | + } |
| 180 | + synchronized (vector) { |
| 181 | + //System.out.println("Notify pipe in!"); |
| 182 | + vector.notifyAll(); |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * |
| 188 | + * @param key |
| 189 | + * @return |
| 190 | + * @j2sIgnore |
| 191 | + */ |
| 192 | + static boolean isPipeLive(String key) { |
| 193 | + SimplePipeRunnable pipe = getPipe(key); |
| 194 | + if (pipe != null) { |
| 195 | + return pipe.isPipeLive(); |
| 196 | + } |
| 197 | + return false; |
| 198 | + } |
| 199 | + |
| 200 | + /** |
| 201 | + * |
| 202 | + * @param key |
| 203 | + * @param live |
| 204 | + * @j2sIgnore |
| 205 | + */ |
| 206 | + static boolean notifyPipeStatus(String key, boolean live) { |
| 207 | + SimplePipeRunnable pipe = getPipe(key); |
| 208 | + if (pipe != null) { |
| 209 | + pipe.updateStatus(live); |
| 210 | + return true; |
| 211 | + } |
| 212 | + return false; |
| 213 | + } |
| 214 | + |
| 215 | +} |
0 commit comments