private void handleRename(
      final GeoServerTileLayerInfo tileLayerInfo,
      final CatalogInfo source,
      final List<String> changedProperties,
      final List<Object> oldValues,
      final List<Object> newValues) {

    final int nameIndex = changedProperties.indexOf("name");
    final int namespaceIndex = changedProperties.indexOf("namespace");

    String oldLayerName;
    String newLayerName;
    if (source instanceof ResourceInfo) { // covers LayerInfo, CoverageInfo, and WMSLayerInfo
      // must cover prefix:name
      final ResourceInfo resourceInfo = (ResourceInfo) source;
      final NamespaceInfo currNamespace = resourceInfo.getNamespace();
      final NamespaceInfo oldNamespace;
      if (namespaceIndex > -1) {
        // namespace changed
        oldNamespace = (NamespaceInfo) oldValues.get(namespaceIndex);
      } else {
        oldNamespace = currNamespace;
      }

      newLayerName = resourceInfo.prefixedName();
      if (nameIndex > -1) {
        oldLayerName = (String) oldValues.get(nameIndex);
      } else {
        oldLayerName = resourceInfo.getName();
      }
      oldLayerName = oldNamespace.getPrefix() + ":" + oldLayerName;
    } else {
      // it's a layer group, no need to worry about namespace
      oldLayerName = tileLayerInfo.getName();
      newLayerName = tileLayerName((LayerGroupInfo) source);
    }

    if (!oldLayerName.equals(newLayerName)) {
      tileLayerInfo.setName(newLayerName);

      // notify the mediator of the rename so it changes the name of the layer in GWC without
      // affecting its caches
      GridSetBroker gridSetBroker = mediator.getGridSetBroker();

      final GeoServerTileLayer oldTileLayer =
          (GeoServerTileLayer) mediator.getTileLayerByName(oldLayerName);

      checkState(
          null != oldTileLayer,
          "hanldeRename: old tile layer not found: '"
              + oldLayerName
              + "'. New name: '"
              + newLayerName
              + "'");

      final GeoServerTileLayer modifiedTileLayer;

      if (oldTileLayer.getLayerInfo() != null) {
        LayerInfo layerInfo = oldTileLayer.getLayerInfo();
        modifiedTileLayer = new GeoServerTileLayer(layerInfo, gridSetBroker, tileLayerInfo);
      } else {
        LayerGroupInfo layerGroup = oldTileLayer.getLayerGroupInfo();
        modifiedTileLayer = new GeoServerTileLayer(layerGroup, gridSetBroker, tileLayerInfo);
      }
      mediator.save(modifiedTileLayer);
    }
  }
 @Override
 protected TileLayer load() {
   GWC facade = GWC.get();
   return facade.getTileLayerByName(name);
 }
  GridSubset findBestGridSubset(WMSMapContent map) {
    GetMapRequest req = map.getRequest();
    Map formatOpts = req.getFormatOptions();

    GridSetBroker gridSetBroker = gwc.getGridSetBroker();
    GridSet gridSet = null;

    // first check format options to see if explicitly specified
    if (formatOpts.containsKey("gridset")) {
      gridSet = gridSetBroker.get(formatOpts.get("gridset").toString());
    }

    // next check srs
    if (gridSet == null) {
      gridSet = gridSetBroker.get(req.getSRS().toUpperCase());
    }

    if (gridSet != null) {
      return GridSubsetFactory.createGridSubSet(gridSet);
    }

    CoordinateReferenceSystem crs = map.getCoordinateReferenceSystem();

    // look up epsg code
    Integer epsgCode = null;
    try {
      epsgCode = CRS.lookupEpsgCode(crs, false);
    } catch (Exception e) {
      throw new ServiceException("Unable to determine epsg code for " + crs, e);
    }
    if (epsgCode == null) {
      throw new ServiceException("Unable to determine epsg code for " + crs);
    }

    SRS srs = SRS.getSRS(epsgCode);

    // figure out the appropriate grid sub set
    Set<GridSubset> gridSubsets = new LinkedHashSet<GridSubset>();
    for (MapLayerInfo l : req.getLayers()) {
      TileLayer tl = gwc.getTileLayerByName(l.getName());
      if (tl == null) {
        throw new ServiceException("No tile layer for " + l.getName());
      }

      List<GridSubset> theseGridSubsets = tl.getGridSubsetsForSRS(srs);
      if (gridSubsets.isEmpty()) {
        gridSubsets.addAll(theseGridSubsets);
      } else {
        gridSubsets.retainAll(theseGridSubsets);
      }

      if (gridSubsets.isEmpty()) {
        throw new ServiceException(
            "No suitable " + epsgCode + " grid subset for " + req.getLayers());
      }
    }

    if (gridSubsets.size() > 1) {
      if (LOGGER.isLoggable(Level.WARNING)) {
        StringBuilder msg = new StringBuilder("Found multiple grid subsets: ");
        for (GridSubset gs : gridSubsets) {
          msg.append(gs.getName()).append(", ");
        }
        msg.setLength(msg.length() - 2);
        msg.append(". Choosing first.");
        LOGGER.warning(msg.toString());
      }
    }

    return gridSubsets.iterator().next();
  }