Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1eafb3dd4f98b1f80054488589a93a2869630b61 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2007, 2010 Wind River Systems, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.internal.debug.launch;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.runtime.IPath;
import org.eclipse.tcf.internal.debug.Activator;
import org.eclipse.tcf.core.AbstractPeer;
import org.eclipse.tcf.protocol.IPeer;
import org.eclipse.tcf.protocol.Protocol;

/**
 * The class represents manually configured (user defined) TCF peers (targets).
 * Unlike auto-discovered peers, manually configured ones are persistent -
 * they exist until explicitly deleted by user.
 * Eclipse plug-in state storage is used to keep the configuration data.
 */
public class TCFUserDefPeer extends AbstractPeer {

    public TCFUserDefPeer(Map<String, String> attrs) {
        super(attrs);
    }

    /**
     * Load manually configured peers from persistent storage.
     */
    public static void loadPeers() {
        try {
            assert Protocol.isDispatchThread();
            IPath path = Activator.getDefault().getStateLocation();
            File f = path.append("peers.ini").toFile();
            if (!f.exists()) return;
            HashMap<String,String> attrs = new HashMap<String,String>();
            BufferedReader rd = new BufferedReader(new InputStreamReader(new FileInputStream(f), "UTF-8"));
            for (;;) {
                String s = rd.readLine();
                if (s == null) break;
                if (s.length() == 0) {
                    new TCFUserDefPeer(attrs);
                    attrs = new HashMap<String,String>();
                }
                else {
                    int i = s.indexOf('=');
                    if (i > 0) attrs.put(s.substring(0, i), s.substring(i + 1));
                }
            }
            rd.close();
        }
        catch (Exception x) {
            Activator.log("Cannot read peer list", x);
        }
    }

    /**
     * Save manually configured peers to persistent storage.
     */
    public static void savePeers() {
        try {
            assert Protocol.isDispatchThread();
            IPath path = Activator.getDefault().getStateLocation();
            File f = path.append("peers.ini").toFile();
            BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f), "UTF-8"));
            for (IPeer peer : Protocol.getLocator().getPeers().values()) {
                if (peer instanceof TCFUserDefPeer) {
                    Map<String,String> attrs = peer.getAttributes();
                    for (String nm : attrs.keySet()) {
                        wr.write(nm);
                        wr.write('=');
                        wr.write(attrs.get(nm));
                        wr.newLine();
                    }
                    wr.newLine();
                }
            }
            wr.close();
        }
        catch (Exception x) {
            Activator.log("Cannot save peer list", x);
        }
    }
}

Back to the top