/** * @param garFile GAR file. * @param hasDescr Whether GAR file has descriptor. * @throws IOException If GAR file is not found. * @return Whether GAR file structure is correct. */ private boolean checkStructure(File garFile, boolean hasDescr) throws IOException { ZipFile zip = new ZipFile(garFile); String descr = "META-INF/ignite.xml"; ZipEntry entry = zip.getEntry(descr); if (entry == null && !hasDescr) { if (log().isInfoEnabled()) { log() .info( "Process deployment without descriptor file [path=" + descr + ", file=" + garFile.getAbsolutePath() + ']'); } return true; } else if (entry != null && hasDescr) { InputStream in = null; try { in = new BufferedInputStream(zip.getInputStream(entry)); return true; } finally { U.close(in, log()); } } else return false; }
/** * Read block from file. * * @param file - File to read. * @param off - Marker position in file to start read from if {@code -1} read last blockSz bytes. * @param blockSz - Maximum number of chars to read. * @param lastModified - File last modification time. * @return Read file block. * @throws IOException In case of error. */ public static VisorFileBlock readBlock(File file, long off, int blockSz, long lastModified) throws IOException { RandomAccessFile raf = null; try { long fSz = file.length(); long fLastModified = file.lastModified(); long pos = off >= 0 ? off : Math.max(fSz - blockSz, 0); // Try read more that file length. if (fLastModified == lastModified && fSz != 0 && pos >= fSz) throw new IOException( "Trying to read file block with wrong offset: " + pos + " while file size: " + fSz); if (fSz == 0) return new VisorFileBlock(file.getPath(), pos, fLastModified, 0, false, EMPTY_FILE_BUF); else { int toRead = Math.min(blockSz, (int) (fSz - pos)); byte[] buf = new byte[toRead]; raf = new RandomAccessFile(file, "r"); raf.seek(pos); int cntRead = raf.read(buf, 0, toRead); if (cntRead != toRead) throw new IOException( "Count of requested and actually read bytes does not match [cntRead=" + cntRead + ", toRead=" + toRead + ']'); boolean zipped = buf.length > 512; return new VisorFileBlock( file.getPath(), pos, fSz, fLastModified, zipped, zipped ? zipBytes(buf) : buf); } } finally { U.close(raf, null); } }
/** * Converts given input stream expecting XML inside to {@link GridUriDeploymentSpringDocument}. * * <p>This is a workaround for the {@link InputStreamResource} which does not work properly. * * @param in Input stream with XML. * @param log Logger * @return Grid wrapper for the input stream. * @throws GridSpiException Thrown if incoming input stream could not be read or parsed by {@code * Spring} {@link XmlBeanFactory}. * @see XmlBeanFactory */ static GridUriDeploymentSpringDocument parseTasksDocument(InputStream in, GridLogger log) throws GridSpiException { assert in != null; // Note: use ByteArrayResource instead of InputStreamResource because InputStreamResource // doesn't work. ByteArrayOutputStream out = new ByteArrayOutputStream(); try { U.copy(in, out); XmlBeanFactory factory = new XmlBeanFactory(new ByteArrayResource(out.toByteArray())); return new GridUriDeploymentSpringDocument(factory); } catch (BeansException e) { throw new GridSpiException("Failed to parse spring XML file.", e); } catch (IOException e) { throw new GridSpiException("Failed to parse spring XML file.", e); } finally { U.close(out, log); } }
/** * Checks availability of a classpath resource. * * @param name Resource name. * @return {@code true} if resource is available and ready for read, {@code false} otherwise. */ private boolean resourceAvailable(String name) { InputStream cfgStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(name); if (cfgStream == null) { log.error("Classpath resource not found: " + name); return false; } try { // Read a single byte to force actual content access by JVM. cfgStream.read(); return true; } catch (IOException e) { log.error("Failed to read classpath resource: " + name, e); return false; } finally { U.close(cfgStream, log); } }
/** Clears values for this partition. */ private void clearAll() { GridCacheVersion clearVer = cctx.versions().next(); boolean swap = cctx.isSwapOrOffheapEnabled(); boolean rec = cctx.events().isRecordable(EVT_CACHE_REBALANCE_OBJECT_UNLOADED); Iterator<GridDhtCacheEntry> it = map.values().iterator(); GridCloseableIterator<Map.Entry<byte[], GridCacheSwapEntry>> swapIt = null; if (swap && GridQueryProcessor.isEnabled(cctx.config())) { // Indexing needs to unswap cache values. Iterator<GridDhtCacheEntry> unswapIt = null; try { swapIt = cctx.swap().iterator(id); unswapIt = unswapIterator(swapIt); } catch (Exception e) { U.error(log, "Failed to clear swap for evicted partition: " + this, e); } if (unswapIt != null) it = F.concat(it, unswapIt); } try { while (it.hasNext()) { GridDhtCacheEntry cached = it.next(); try { if (cached.clearInternal(clearVer, swap)) { map.remove(cached.key(), cached); if (!cached.isInternal()) { mapPubSize.decrement(); if (rec) cctx.events() .addEvent( cached.partition(), cached.key(), cctx.localNodeId(), (IgniteUuid) null, null, EVT_CACHE_REBALANCE_OBJECT_UNLOADED, null, false, cached.rawGet(), cached.hasValue(), null, null, null); } } } catch (IgniteCheckedException e) { U.error(log, "Failed to clear cache entry for evicted partition: " + cached, e); } } } finally { U.close(swapIt, log); } }