View Javadoc

1   /**
2    * Copyright (C) 2008 Atlassian
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.atlassian.theplugin.idea.config.serverconfig.defaultCredentials;
17  
18  import com.atlassian.theplugin.idea.config.serverconfig.ProductConnector;
19  import com.atlassian.theplugin.idea.TestConnectionProcessor;
20  import com.atlassian.theplugin.ConnectionWrapper;
21  import com.atlassian.theplugin.util.PluginUtil;
22  import com.atlassian.theplugin.commons.bamboo.BambooServerFacadeImpl;
23  import com.atlassian.theplugin.commons.crucible.CrucibleServerFacadeImpl;
24  import com.atlassian.theplugin.jira.JIRAServerFacadeImpl;
25  
26  /**
27   * User: pmaruszak
28   */
29  public class TestConnectionThread extends Thread {
30  	private static final int CHECK_CANCEL_INTERVAL = 500;
31  	private final TestConnectionProcessor testConnectionProcessor;
32  	private final ServerDataExt server;
33  
34  	public TestConnectionThread(@org.jetbrains.annotations.NotNull String s, TestConnectionProcessor testConnectionProcessor,
35  			ServerDataExt server) {
36  		super(s);
37  		this.testConnectionProcessor = testConnectionProcessor;
38  		this.server = server;
39  	}
40  
41  	@Override
42  	public void run() {
43  			ProductConnector productConnector;
44  			ConnectionWrapper testConnector;
45  			switch (server.getServerType()) {
46  				case JIRA_SERVER:
47  					productConnector = new ProductConnector(JIRAServerFacadeImpl.getInstance());
48  					break;
49  
50  				case BAMBOO_SERVER:
51  					productConnector = new ProductConnector(BambooServerFacadeImpl.getInstance(PluginUtil.getLogger()));
52  					break;
53  
54  				case CRUCIBLE_SERVER:
55  				case FISHEYE_SERVER:
56  					productConnector = new ProductConnector(CrucibleServerFacadeImpl.getInstance());
57  					break;
58  
59  				default:
60  					return;
61  
62  			}
63  			testConnector = new ConnectionWrapper(productConnector, server.getServerData(), "testing connection");
64  
65  			testConnector.start();
66  			while (testConnector.getConnectionState() == ConnectionWrapper.ConnectionState.NOT_FINISHED) {
67  				try {
68  					if (false) {
69  						testConnector.setInterrupted();
70  						//t.interrupt();
71  						break;
72  					} else {
73  						java.lang.Thread.sleep(CHECK_CANCEL_INTERVAL);
74  					}
75  				} catch (InterruptedException e) {
76  					//log.info(e.getMessage());
77  				}
78  			}
79  
80  			if (testConnector.getConnectionState() == ConnectionWrapper.ConnectionState.SUCCEEDED) {
81  				testConnectionProcessor.onSuccess();
82  			} else {
83  				testConnectionProcessor.onError(testConnector.getErrorMessage());
84  			}
85  
86  		}
87  
88  
89  }