001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.lib.collab.xmpp;
043:
044: import java.util.Iterator;
045: import java.util.LinkedList;
046: import java.util.List;
047: import java.util.Map;
048: import java.util.TreeMap;
049: import org.jabberstudio.jso.NSI;
050: import org.jabberstudio.jso.Stream;
051: import org.jabberstudio.jso.StreamException;
052: import org.jabberstudio.jso.StreamFeature;
053: import org.jabberstudio.jso.event.PacketEvent;
054: import org.jabberstudio.jso.event.StreamFeaturesEvent;
055: import org.jabberstudio.jso.event.StreamFeaturesListener;
056:
057: /**
058: *
059: * @author Mridul Muralidharan
060: */
061: public class FeatureIdentifier implements StreamFeaturesListener {
062:
063: private List featureList;
064: private Stream stream;
065: private boolean initialised = false;
066:
067: public FeatureIdentifier(Stream stream) {
068: this .featureList = new LinkedList();
069: this .stream = stream;
070: }
071:
072: public List getFeatureList() {
073: return new LinkedList(featureList);
074: }
075:
076: public void setFeatureList(List list) {
077: if (null != list) {
078: this .featureList = new LinkedList(list);
079: } else {
080: this .featureList = new LinkedList();
081: }
082: }
083:
084: public void resetFeatureList() {
085: featureList.clear();
086: }
087:
088: public synchronized void initialise() throws StreamException {
089: if (null == stream) {
090: throw new IllegalStateException("Stream not specified");
091: }
092: if (!initialised) {
093: initialised = true;
094: stream
095: .addStreamFeaturesListener(PacketEvent.RECEIVED,
096: this );
097: }
098: }
099:
100: public synchronized void release() throws StreamException {
101: /*
102: if (null == stream){
103: throw new IllegalStateException("Stream not specified");
104: }
105: */
106: if (initialised) {
107: initialised = false;
108: stream.removeStreamFeaturesListener(PacketEvent.RECEIVED,
109: this );
110: }
111: }
112:
113: public void process(long timeout) throws StreamException {
114: if (null == stream) {
115: throw new IllegalStateException("Stream not specified");
116: }
117:
118: synchronized (this ) {
119: boolean done = false;
120: long startTime = System.currentTimeMillis();
121: while (!done) {
122: stream.process();
123:
124: if (!featureList.isEmpty()) {
125: break;
126: }
127:
128: int diff = (int) (System.currentTimeMillis() - startTime);
129:
130: // If timeout specified is <= 0 , then infinite loop until all the
131: // specified features are found.
132: if (stream.getCurrentStatus() == stream.CLOSED
133: || (timeout > 0 && diff > timeout)) {
134: XMPPSessionProvider.debug("feature wait timeout");
135: break;
136: }
137: try {
138: wait(10);
139: } catch (InterruptedException ie) {
140: }
141: }
142: }
143: }
144:
145: public void featuresReported(StreamFeaturesEvent evt) {
146:
147: synchronized (this ) {
148: List features = evt.listFeatures();
149:
150: assert (null != features);
151: //featureList.addAll(features);
152: Iterator iter = features.iterator();
153:
154: XMPPSessionProvider.debug("featuresReported : " + evt);
155:
156: while (iter.hasNext()) {
157: StreamFeature sf = (StreamFeature) iter.next();
158: NSI nsi = sf.getNSI();
159:
160: XMPPSessionProvider.debug("featuresReported(nsi) : "
161: + nsi);
162:
163: featureList.add(sf);
164: }
165: }
166: }
167: }
|