View Javadoc

1   /*
2    * Jour - java profiler and monitoring library
3    *
4    * Copyright (C) 2004 Jour team
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Library General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Library General Public License for more details.
15   *
16   * You should have received a copy of the GNU Library General Public
17   * License along with this library; if not, write to the
18   * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19   * Boston, MA  02111-1307, USA.
20   */
21  package net.sf.jour.rt.agent;
22  
23  import java.util.HashMap;
24  import java.util.*;
25  
26  import java.io.*;
27  import java.net.*;
28  import java.io.PrintStream;
29  import java.io.FileOutputStream;
30  import java.io.BufferedReader;
31  
32  import org.apache.log4j.Logger;
33  
34  /***
35   * TODO Add docs
36   * Created on 02.12.2004
37   *
38   * Contributing Author(s):
39   *
40   *   Misha Lifschitz <mishalifschitz at users.sourceforge.net> (Inital implementation)
41   *   Vlad Skarzhevskyy <vlads at users.sourceforge.net> (Inital implementation)
42   *
43   * @author vlads
44   * @version $Revision: 1.6 $ ($Author: vlads $) $Date: 2004/12/09 19:11:35 $
45   */
46  
47  public class InstrumentationMap {
48  
49  	protected final Logger log = Logger.getLogger(InstrumentationMap.class);
50  	
51      private static InstrumentationMap instance = null;
52  
53      public final static String separator = "|";
54      
55  	public final static String DEFULAT_FILE_NAME = "jourInstrumentationMap.txt";
56  
57  	/*
58  	 * TODO use BigMap to contain more than int numbers.
59  	 */
60      private HashMap map;
61      private HashMap mapID;
62      private long maxID = 0;
63  	private boolean updated;
64  
65      public InstrumentationMap() {
66          map = new HashMap();
67  		mapID = new HashMap();
68  		updated = false;
69      }
70  
71      // Static Initializer
72  	static {
73  		instance();
74  	}
75  
76  	/*
77  	 * Returns an instance
78  	 */
79  	public static InstrumentationMap instance() {
80  		// Create a default instance if none was provided
81  		if (instance == null) {
82  			synchronized (InstrumentationMap.class) {
83  				if (instance == null) {
84  					instance = new InstrumentationMap();
85  				}
86  			}
87  		}
88  		return instance;
89  	}
90  
91  	public boolean isUpdated() {
92  		return this.updated;
93  	}
94  	
95  	public long size() {
96  	    return map.size();
97  	}
98  
99  	public long getID(String className, String methodName, String methodSignature) {
100 	    return getID(className + separator + methodName + separator + methodSignature);
101 	}
102 
103     long getID(String name) {
104         if (map.containsKey(name)) {
105 			Long l = (Long)map.get(name);
106 			return l.longValue();
107 		} else {
108 			Long l = getNewID(0);
109 			addID(l, name);
110 			return l.longValue();
111 		}
112     }
113 
114     public Long getNewID(long base) {
115 		// Map starts from 1
116 		long l = maxID;
117 		if (maxID == 0) {
118 			l = mapID.size();
119 		}
120 		if (maxID == 0) {
121 			l = 1;
122 		}
123 		if (base != 0) {
124 			l = base + 1;
125 		}
126 		while (mapID.containsKey(new Long(l))) {
127 			l ++;
128 		}
129 		maxID = l + 1;
130 		return new Long(l);
131 	}
132 
133 	public void addID(Long l, String name) {
134 		addID(l, name, true);
135 	}
136 
137 	private void addID(Long l, String name, boolean stateChange) {
138 			map.put(name, l);
139 			mapID.put(l, name);
140 			if (stateChange) {
141 				updated = true;
142 			}
143 	}
144 		
145 	public String getName(long cid) {
146 		if (mapID.containsKey(new Long(cid))) {
147 			String name = (String)mapID.get(new Long(cid));
148 			return name;
149 		} else {
150 			return "?#" + cid;
151 		}
152 	}
153 
154 	public boolean save(String filename) {
155 		try {
156 			//Iterator itr = map.keySet().iterator();
157 		    Vector sortedMap = new Vector(map.keySet());
158 		    Collections.sort(sortedMap);
159 		    Iterator itr = sortedMap.iterator();
160 
161 			PrintStream out =
162 				new PrintStream(new FileOutputStream(filename, false));
163 
164 			while (itr.hasNext()) {
165 				String name = (String) itr.next();
166 				Long id = (Long) map.get(name);
167 				out.println(id + ";" + name);
168 			}
169 
170 			out.close();
171 			return true;
172 		} catch (Exception e) {
173 			log.error("Error saving map", e);
174 			return false;
175 		}
176 
177 	}
178 
179 	public boolean load(File file) {
180 	    try {
181             return load(new URL("file:" + file.getAbsolutePath()));
182         } catch (MalformedURLException e) {
183             log.error("Error", e);
184             return false;
185         }
186 	}
187 	
188 	public boolean load(URL url) {
189 		try {
190 			if (url == null) {
191 				log.warn("fails to load map, file does not exists");
192 				return false;
193 			}
194 			BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
195 			while (true) {
196 				String l = in.readLine();
197 			    if (l == null) break;
198 				l = l.trim();
199 				if ((l.length() == 0) || (l.startsWith("#"))) {
200 					continue;
201 				}
202 
203                 int idx = l.indexOf(";");
204                 if (idx >= 0) {
205                     String id = l.substring(0, idx);
206                     String name = l.substring(idx + 1);
207                     addID(new Long(id), name, false);
208                 }
209 			}
210 			in.close();
211 			return true;
212 		} catch (Exception e) {
213 			log.error("Error loading map", e);
214 			return false;
215 		}
216 	}
217 }