9376f3f15c48b3f15e43f62a637e4a377fd7b68c
[public/netxms.git] / src / java / netxms-eclipse / SNMP / src / org / netxms / ui / eclipse / snmp / shared / MibCache.java
1 /**
2 * NetXMS - open source network management system
3 * Copyright (C) 2003-2012 Victor Kirhenshtein
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 package org.netxms.ui.eclipse.snmp.shared;
20
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.FileOutputStream;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26 import java.net.URISyntaxException;
27 import java.util.Date;
28 import org.eclipse.core.runtime.IProgressMonitor;
29 import org.eclipse.core.runtime.Platform;
30 import org.eclipse.osgi.service.datalocation.Location;
31 import org.eclipse.swt.widgets.Display;
32 import org.netxms.client.NXCSession;
33 import org.netxms.client.snmp.MibObject;
34 import org.netxms.client.snmp.MibTree;
35 import org.netxms.client.snmp.SnmpObjectId;
36 import org.netxms.client.snmp.SnmpObjectIdFormatException;
37 import org.netxms.ui.eclipse.console.api.ConsoleLoginListener;
38 import org.netxms.ui.eclipse.jobs.ConsoleJob;
39 import org.netxms.ui.eclipse.shared.ConsoleSharedData;
40 import org.netxms.ui.eclipse.snmp.Activator;
41 import org.netxms.ui.eclipse.snmp.Messages;
42
43 /**
44 * SNMP MIB cache
45 */
46 public final class MibCache implements ConsoleLoginListener
47 {
48 private static MibTree mibTree = null;
49
50 /* (non-Javadoc)
51 * @see org.netxms.ui.eclipse.console.api.ConsoleLoginListener#afterLogin(org.netxms.client.NXCSession, org.eclipse.swt.widgets.Display)
52 */
53 @Override
54 public void afterLogin(final NXCSession session, Display display)
55 {
56 Boolean slowLink = (Boolean)ConsoleSharedData.getProperty("SlowLink");
57 if ((slowLink != null) && slowLink.booleanValue())
58 return;
59
60 ConsoleJob job = new ConsoleJob(Messages.get().LoginListener_JobTitle, null, Activator.PLUGIN_ID, null) {
61 @Override
62 protected void runInternal(IProgressMonitor monitor) throws Exception
63 {
64 Location loc = Platform.getInstanceLocation();
65 if (loc != null)
66 {
67 File targetDir;
68 try
69 {
70 targetDir = new File(loc.getURL().toURI());
71 }
72 catch(URISyntaxException e)
73 {
74 targetDir = new File(loc.getURL().getPath());
75 }
76 File mibFile = new File(targetDir, "netxms.mib"); //$NON-NLS-1$
77
78 Date serverMibTimestamp = session.getMibFileTimestamp();
79 if (!mibFile.exists() || (serverMibTimestamp.getTime() > mibFile.lastModified()))
80 {
81 File file = session.downloadMibFile();
82 Activator.logInfo("MIB file downloaded to: " + file.getPath() + " (size " + file.length() + " bytes)"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
83
84 if (mibFile.exists())
85 mibFile.delete();
86
87 if (!file.renameTo(mibFile))
88 {
89 // Rename failed, try to copy file
90 InputStream in = null;
91 OutputStream out = null;
92 try
93 {
94 in = new FileInputStream(file);
95 out = new FileOutputStream(mibFile);
96 byte[] buffer = new byte[16384];
97 int len;
98 while((len = in.read(buffer)) > 0)
99 out.write(buffer, 0, len);
100 }
101 catch(Exception e)
102 {
103 throw e;
104 }
105 finally
106 {
107 if (in != null)
108 in.close();
109 if (out != null)
110 out.close();
111 }
112
113 file.delete();
114 }
115 }
116 }
117 }
118
119 @Override
120 protected String getErrorMessage()
121 {
122 return Messages.get().LoginListener_JobError;
123 }
124 };
125 job.setUser(false);
126 job.start();
127 }
128
129 /**
130 * @return the mibTree
131 */
132 public static MibTree getMibTree()
133 {
134 if (mibTree != null)
135 return mibTree;
136
137 Location loc = Platform.getInstanceLocation();
138 if (loc != null)
139 {
140 File targetDir;
141 try
142 {
143 targetDir = new File(loc.getURL().toURI());
144 }
145 catch(URISyntaxException e)
146 {
147 targetDir = new File(loc.getURL().getPath());
148 }
149 File mibFile = new File(targetDir, "netxms.mib"); //$NON-NLS-1$
150 if (mibFile.exists())
151 {
152 try
153 {
154 mibTree = new MibTree(mibFile);
155 }
156 catch(Exception e)
157 {
158 Activator.logError("Cannot load MIB file", e); //$NON-NLS-1$
159 }
160 }
161 }
162 return (mibTree != null) ? mibTree : new MibTree();
163 }
164
165 /**
166 * Find matching object in tree. If exactMatch set to true, method will search for object with
167 * ID equal to given. If exactMatch set to false, and object with given id cannot be found, closest upper level
168 * object will be returned (i.e., if object .1.3.6.1.5 does not exist in the tree, but .1.3.6.1 does, .1.3.6.1 will
169 * be returned in search for .1.3.6.1.5).
170 *
171 * @param oid object id to find
172 * @param exactMatch set to true if exact match required
173 * @return MIB object or null if matching object not found
174 */
175 public static MibObject findObject(String oid, boolean exactMatch)
176 {
177 if (mibTree == null)
178 return null;
179
180 SnmpObjectId id;
181 try
182 {
183 id = SnmpObjectId.parseSnmpObjectId(oid);
184 }
185 catch(SnmpObjectIdFormatException e)
186 {
187 return null;
188 }
189 return mibTree.findObject(id, exactMatch);
190 }
191 }